We can define intrinsic operators of Python as stated here. Just for curiosity, can we define new operators like $ or ***? (If so, then we can define ternary condition operators or rotate operators.) Answer Expanding on @fasouto answer, but adding a bit more code. While you cannot define new operators AND you…
Tag: python
Creating seed data in a flask-migrate or alembic migration
How can I insert some seed data in my first migration? If the migration is not the best place for this, then what is the best practice? Answer Alembic has, as one of its operation, bulk_insert(). The documentation gives the following example (with some fixes I’ve included): Note too that the alembic has…
How do I find directory of the Python running script from inside the script?
How do I find the directory of the running Python script from inside Python [3.3]? I have tried what was suggested at: How can I find script’s directory? , but I get “Invalid syntax” and directed to “os” (And I did import os). The closest I have got to the answer is: sys.argv[0],…
Index all *except* one item in python
Is there a simple way to index all elements of a list (or array, or whatever) except for a particular index? E.g., mylist[3] will return the item in position 3 milist[~3] will return the whole list except for 3 Answer For a list, you could use a list comp. For example, to make b a copy of a without the
Is there a short-hand for nth root of x in Python?
In maths, if I wish to calculate 3 to the power of 2 then no symbol is required, but I write the 2 small: 3². In Python this operation seems to be represented by the ** syntax. If I want to go the other direction and calculate the 2nd root of 9 then in maths I need to use a
sklearn plot confusion matrix with labels
I want to plot a confusion matrix to visualize the classifer’s performance, but it shows only the numbers of the labels, not the labels themselves: How can I add the labels (health, business..etc) to the confusion matrix? Answer As hinted in this question, you have to “open” the lower-level …
Change one value based on another value in pandas
I’m trying to reproduce my Stata code in Python, and I was pointed in the direction of Pandas. I am, however, having a hard time wrapping my head around how to process the data. Let’s say I want to iterate over all values in the column head ‘ID.’ If that ID matches a specific number, t…
How do I write a group_concat function in sqlalchemy?
I am trying to re-write this mysql query in SQLAlchemy: Schema: Query: Here’s what I have, but I keep getting: Answer Defining a custom function element seems the easiest way to take care of the group_concat method. And use it something like this:
how to delete json object using python?
I am using python to delete and update a JSON file generated from the data provided by user, so that only few items should be stored in the database. I want to delete a particular object from the JSON file. My JSON file is: I want to delete the JSON object with ename mark. As I am new to python
Get psycopg2 count(*) number of results
Whats the correct way to get the number or rows returned by this query? I’m specifically looking to see if no results are returned. Thanks. Answer results is itself a row object, in your case (judging by the claimed print output), a dictionary (you probably configured a dict-like cursor subclass); simpl…