Skip to content

Instantly share code, notes, and snippets.

View ammaaim's full-sized avatar

Alex Balan ammaaim

View GitHub Profile
@ammaaim
ammaaim / flask-get.py
Created June 26, 2016 11:30
GET method arguments in Flask
from flask import request
@app.route('/data')
def data():
# here we want to get the value of user (i.e. ?user=some-value)
user = request.args.get('user')
@ammaaim
ammaaim / read-serial.py
Last active January 9, 2016 16:15
Serial port raw reader
# $ pip install pyserial
import serial
ser = serial.Serial('/dev/tty.usbmodem1411') # open serial port
while True:
x = ser.read()
print(int(ord(x))) # print received byte as int value
@ammaaim
ammaaim / weasyprint.txt
Last active April 18, 2017 10:36
Python3 HTML to PDF generator
>>> INSTALL
pip3.4 install weasyprint
>>> EXAMPLE
from weasyprint import HTML, CSS
html_src='''
<h1>The title</h1>
<p>Content goes here
@ammaaim
ammaaim / pip-mac.txt
Created September 8, 2015 12:09
Howto install Python PIP on MacOS
sudo easy_install pip
@ammaaim
ammaaim / bsd_fopen.txt
Created September 3, 2015 07:03
Open Flags
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):
``r'' Open text file for reading. The stream is positioned at the
beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
``w'' Truncate file to zero length or create text file for writing.
@ammaaim
ammaaim / yaml.rb
Last active December 12, 2018 12:41
Read/Write YAML in Ruby
require 'yaml'
$db_file = 'filename.yaml'
def load_data
text = File.read 'db.yaml'
return YAML.load text
end
def store_data hash
@ammaaim
ammaaim / create-folder
Created July 20, 2015 08:36
Create folder if not exist
directory_name = "name"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
@ammaaim
ammaaim / remove-all-older.sql
Created December 10, 2014 13:08
remove all older rows sql
-- FOR MySQL -disable safemode
SET SQL_SAFE_UPDATES = 0;
-- for example remove all older than '2014-10-01'
DELETE FROM `base-name`.`table-name` WHERE `table-name`.`date-field` < '2014-10-01';
@ammaaim
ammaaim / rails-import-from-csv.rb
Created November 12, 2014 13:36
ruby import model data from csv
require 'csv'
csv_text = File.read('filename.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
Model.create!(row.to_hash)
end
@ammaaim
ammaaim / ruby-csv.rb
Last active August 29, 2015 14:09
ruby csv example
# in excel: file->import->CSV file
require 'csv'
# write csv
CSV.open("file.csv", "wb") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end