Skip to content

Instantly share code, notes, and snippets.

@Khouli00
Khouli00 / datetime.py
Last active March 2, 2020 20:49
datetime
#serialize datetime to json
#https://stackoverflow.com/questions/35869985/datetime-datetime-is-not-json-serializable
import datetime
import json
def datetime_handler(x):
if isinstance(x, datetime.datetime):
return x.isoformat()
raise TypeError("Unknown type")
@Khouli00
Khouli00 / json.py
Last active February 22, 2017 14:43
#UTF-8 proof
with open(os.path.join('test','graph.json'),'w',encoding='utf-8') as fd:
json.dump(json_graph.adjacency_data(Tree.graph),fd,indent=1,ensure_ascii=False)
@Khouli00
Khouli00 / list.py
Last active November 17, 2016 19:57
import itertools
#collapse list
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
merged = list(itertools.chain.from_iterable(list2d))
#efficient merge list
#courtesy http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python#952946
l = [[1,2,3],[4,5,6], [7], [8,9]]
C = [item for sublist in l for item in sublist]
#add leading zero ...
print str(1).zfill(2)
print str(10).zfill(2)
>>01
>>10
#format https://pyformat.info/
'{:.2f}'.format(3.141592653589793)
>>'3.14'
@Khouli00
Khouli00 / woo_report.sql
Last active April 10, 2016 08:15
Extract sum-up from woocomerce (weird) DB (crafted for Clem)
-- Order report
--[u'OrderId', u'FirstName', u'LastName', u'Email', u'Address', u'Postcode', u'ProductList', u'Status', u'PaidDate', u'CompletedDate', u'PaymentMethod', u'Shipping', u'TotalOrder']
SELECT post_id AS OrderId,
Group_concat( IF( meta_key = '_billing_first_name', meta_value, NULL ) ) AS FirstName,
Group_concat( IF( meta_key = '_billing_last_name', meta_value, NULL ) ) AS LastName,
Group_concat( IF(meta_key = '_billing_email', meta_value, NULL)) AS Email,
Group_concat( IF( meta_key = '_billing_address_1', meta_value, NULL ) ) AS Address,
Group_concat( IF( meta_key = '_billing_postcode', meta_value, NULL ) ) AS Postcode,
Group_concat( IF( meta_key = '_desc_content', meta_value, NULL ) ) AS ProductList,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def pandas_df_to_markdown_table(df,name):
fmt = ['---' for i in range(len(df.columns))]
df_fmt = pd.DataFrame([fmt], columns=df.columns)
df_formatted = pd.concat([df_fmt, df])
df_formatted.to_csv(name,sep="|", index=False)
@Khouli00
Khouli00 / dict_to_table.html
Last active September 20, 2016 20:21 — forked from wrunk/jinja2_file_less.py
python jinja2 examples
<table>
{% for key, value in result.iteritems() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
@Khouli00
Khouli00 / latency.txt
Created January 31, 2017 17:55 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@Khouli00
Khouli00 / postgres-cheatsheet.md
Last active April 23, 2017 07:13 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

If run with -E flag, it will describe the underlaying queries of the \ commands (cool for learning!).

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*