Sqlite and Python — return a dictionary using fetchone()?

There is actually an option for this in sqlite3. Change the row_factory member of the connection object to sqlite3.Row: conn = sqlite3.connect(‘db’, row_factory=sqlite3.Row) or conn.row_factory = sqlite3.Row This will allow you to access row elements by name–dictionary-style–or by index. This is much more efficient than creating your own work-around.

Why is imperative mood important for docstrings?

From the docstring of check_imperative_mood itself: “””D401: First line should be in imperative mood: ‘Do’, not ‘Does’. [Docstring] prescribes the function or method’s effect as a command: (“Do this”, “Return that”), not as a description; e.g. don’t write “Returns the pathname …”. (We’ll ignore the irony that this docstring itself would fail the test.)

How to add command line arguments with flags in Python3?

The python 3 library includes 3 modules for parsing the command line thus nothing extra to add to your setup. The one you should use is argparse import argparse parser = argparse.ArgumentParser() #-db DATABASE -u USERNAME -p PASSWORD -size 20 parser.add_argument(“-db”, “–hostname”, help=”Database name”) parser.add_argument(“-u”, “–username”, help=”User name”) parser.add_argument(“-p”, “–password”, help=”Password”) parser.add_argument(“-size”, “–size”, help=”Size”, type=int) … Read more

Count lines of code in directory using Python

Here’s a function I wrote to count all lines of code in a python package and print an informative output. It will count all lines in all .py import os def countlines(start, lines=0, header=True, begin_start=None): if header: print(‘{:>10} |{:>10} | {:<20}’.format(‘ADDED’, ‘TOTAL’, ‘FILE’)) print(‘{:->11}|{:->11}|{:->20}’.format(”, ”, ”)) for thing in os.listdir(start): thing = os.path.join(start, thing) if … Read more

Sum array by number in numpy

The numpy function bincount was made exactly for this purpose and I’m sure it will be much faster than the other methods for all sizes of inputs: data = [1,2,3,4,5,6] ids = [0,0,1,2,2,1] np.bincount(ids, weights=data) #returns [3,9,9] as a float64 array The i-th element of the output is the sum of all the data elements … Read more

Using conditional to generate new column in pandas dataframe

You can define a function which returns your different states “Full”, “Partial”, “Empty”, etc and then use df.apply to apply the function to each row. Note that you have to pass the keyword argument axis=1 to ensure that it applies the function to rows. import pandas as pd def alert(row): if row[‘used’] == 1.0: return … Read more

error code: 521