Posts

Showing posts from October, 2019

What is a repository

A data structure which stores metadata for a set of files or directory structure. In python it's a collection of files

Get started with Python on Mac

These are the steps Installing Python If you want to run an existing project or package from another source (not your own code) Installing packages from Github If you want to get coding: Fire up IDLE and use that as your coding tools Other more advanced tools do also exist

Installing packages from GitHub

Make sure that you have Pyhon3 and PIP/PIP3 In Terminal run this line: pip3 install git + https :// github . com / jkbr / httpie . git The above is an example of httpie package. Replace the URL with the relevante package OR... You can also install Python packages from Github by cloning the repository. First run this via Terminal in the desired folder to download: git clone https :// github . com / jkbr / httpie . git Then start the installation by running whatever setup or install file is associated: sudo python setup . py install Tip: When your in Terminal, use the "LS" command to show whats in the directory. 

How to read from a table in MSSQL with Python

I use this code import pyodbc conn = pyodbc.connect( 'driver={ODBC Driver 17 for SQL Server};' 'server=XXXXXXXXXXXX;' 'database=YYYYYYYYYYY;' 'uid=ZZZZZZZZZZZ;' 'pwd=QQQQQQQQQQ;' ) rs = conn.cursor() rs.execute( 'SELECT top 10 * FROM quant_test with (NOLOCK)' ) for row in rs: print (row)

How to insert in MSSQL with Python

I used this code that seems to do the trick: import pyodbc conn = pyodbc.connect( 'driver={ODBC Driver 17 for SQL Server};' 'server=XXXXXXXXXX;' 'database=YYYYYY;' 'uid=ZZZZZZ;' 'pwd=QQQQQQQQ;' ) rs = conn.cursor() rs.execute( ''' INSERT INTO quant_test (symbol,stockprice) VALUES ('AAPL',180) ''' ) conn.commit() rs.execute(''' INSERT INTO quant_test (symbol,stockprice) VALUES ('AAPL',180) ''') conn.commit()

How to make a script that refreshes / loades every n-second in Python

I used this simple code: import time import datetime intervalInSeconds = 1.0 starttime=time.time() while True : currentTime = datetime.datetime.now() print (currentTime) time.sleep(intervalInSeconds - ((time.time() - starttime) % intervalInSeconds))

Installing Microsoft ODBC Driver to connect to SQL from Python on Mac OS X

What a nightmare it's been for me to figure this out. Remember to have homebrew installed in advance. Then run this in terminal: brew install msodbcsql At first, it didn't work for me, saying something about taps was empty and no available formulas. Then I found out I needed to run this command in Terminal rm -rf /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core; brew update Then I could run brew install msodbcsql ... but not successfully. OK, so I tried another approach /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release brew update  brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release followed by...   brew install msodbcsql17 mssql-tools This seems to have worked! My driver is now  ODBC Driver 17 for SQL Server Ressources: https://do...

Am I running 32 og 64 bit Python on my Mac?

Use the following code import struct print (struct.calcsize( "P" ) * 8 , "bit version" ) Output: 64 bit version

How to run a Python program on Mac

Just open Terminal and enter the path to your script: Example: /Users/imac/venv/bin/python /Users/imac/PycharmProjects/Sql2/Test.py It executes immediately. 

What is Homebrew and how do I get started on Mac?

It's a free and open-source package management system. It makes it more easy and simple to install software om the macOS operating system (and Linux). Created by Max Howell it's very popular now. Example (run from Terminal) on installing something: brew install unixodbc To install Homebrew, just type this into your terminal: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Connection to MSSQL with Pythonan and pyodbc (on Mac OS X)

There are many python SQL drivers available. Microsoft however places its efforts and confidence in pyodbc driver. If the library pyodbc is not installed, do the following Open Terminal Run this command: pip3 install pyodbc The following will be the result brians-air:~ imac$ pip3 install pyodbc Collecting pyodbc   Downloading https://files.pythonhosted.org/packages/92/91/c0c473491b49a5492f911b745d1388da9c60dd152a93841dc90cf21d0e97/pyodbc-4.0.27-cp37-cp37m-macosx_10_9_x86_64.whl (63kB)     100% |████████████████████████████████| 71kB 237kB/s   Installing collected packages: pyodbc Successfully installed pyodbc-4.0.27 You are using pip version 19.0.3, however version 19.2.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. It's now installed. But does it work? No... Not at first! At least not for me. That didn't work for me and when trying to import the library, this was the result. Input: im...

BMI calculator in Python

This is just a simple BMI calculator coded in Python. name = "Johnny" height_m = 2 weight_kg = 90 bmi = weight_kg / (height_m ** 2 ) print ( "bmi: " ) print (bmi) if bmi < 25 : print (name + ' is not overweight' ) else : print (name + ' is overweight' ) As a function: name1 = "Brian" height_m1 = 2 weight_kg1 = 90 name2 = "Brian's sister" height_m2 = 1.8 weight_kg2 = 160 def bmi_calculator (name , height_m , weight_kg): bmi = weight_kg / (height_m ** 2 ) print ( "bmi: " ) print (bmi) if bmi < 25 : return name + " is not overweight" else : return name + " is overweight" result1 = bmi_calculator(name1 , height_m1 , weight_kg1) print (result1) print (bmi_calculator(name2 , height_m2 , weight_kg2))

If- Then- Else in Python

Examples (if clause): a = 1 b = 2 if a < b: print ( "a is less than b" ) Output: a is less than b Indents (4 spaces) are important (see example below)! Another example Here the A and B variables are changed so the If-clause is not true.  a = 3 b = 2 if a < b: print ( "a is less than b" ) print ( "Something else, unrelated" ) Output: Something else, unrelated Yet another example c = 3 d = 4 if c < d: print ( "c is less than d" ) else : print ( "c is NOT less than d" ) print ( "outside the if block" ) Output: c is less than d outside the if block Else-if example e = 7 f = 8 if e < f: print ( "e is less than f" ) elif e == f: print ( "e is equal to f" ) else : print ( "e is greater than f" ) Output: e is less than f Several ELSE-IF example e = 7 f = 8 if e < f: print ( "e is less than f" ) e...

Good resources for Python coding (getting started with examples and test problems)

I would recommend the following pages: https://codingbat.com/python  <-- many fun challanges https://projecteuler.net/archives  <-- more mathematical oriented and language independent 

Mod operator in Python

The Modulus operator The mod operator gives you the reminder you get by dividing the first number with the second number: 5 % 3 (Five mod Three) Output: 2 This is asking what the remainder you get when you divide five by three... x mod y = r x (dividend) y (divisor) r (remainder) 2 The math is this:  5 / 3 = 1.66 Round that down = 1 1 * 3 = 3 The difference between 5 and 3 = 2  A remainder in mathematics is what's left over in a division problem. When you divide the number you divide up is calle dthe Dividend. The number you are dividiing by is called the Divisor. The number has to be Integer Example with a function: print ( 'Mod operator in Python...' ) print ( 5 % 3 ) def count_events (nums): count = 0 for x in nums: if x % 2 == 0 : count += 1 return count print (count_events([ 2 , 3 , 1 , 3 , 1 , 3 , 3 , 1 , 7 , 23 , 8 ])) Output: 2

Functions in Python (def)

Simple Hello-name function in Python def hello_name (name): return 'Hello ' + name + '!' To execute: print (hello_name( 'Peter' )) The output will be: Hello Peter! ________ Other examples of functions in Python: def string_times (str , n): return str * n or... def first_last6 (nums): if nums([ 0 ]) == 6 or nums[- 1 ] == 6 : return True else : return False or... def sleep_in (weekday , vacation): if weekday == False or vacation == True : return True else : return False or (simplified from above)... def sleep_in2 (weekday , vacation): if not weekday or vacation: return True else : return False or... def first_last6 (nums): if nums([ 0 ]) == 6 or nums[- 1 ] == 6 : return True else : return False

Creating your first "Hello World" Python project in PyCharm on Mac

Create a new project Name it "Hello" Before hitting "Create" make sure the interpreter is Python 3.7 Smash that Create button Under "Project" right click the "Hello" Click "New" --> "Python file" Name it "Hello" and press enter... you now have a Hello.py file Your ready to write some code! Write the following print ( "Hello world" ) To execute the code, right-click and press "Run 'Hello'" This is the output: _________ /Users/imac/venv/bin/python /Users/imac/PycharmProjects/Hello/Hello.py Hello world Process finished with exit code 0 __________ To run the script copy the URL line (save the file first) and then paste /Users/imac/venv/bin/python /Users/imac/PycharmProjects/Hello/Hello.py to execute the script in terminal. Output: ___________ Last login: Fri Oct   4 21:52:51 on ttys001 brian-air:~ imac$ /Users/imac/venv/bin/python /Users/im...

What is PIP in Python?

Package manager. Add libraries and modules to Python. Run it from Mac Terminal by typing Pip3

What is Idle3 in Python?

IDLE in Python is an acronym for Integrated Development and Learning Environment. It's used like and IDE to code, and run python programs. You can launch if from your Mac terminal by typing "Idle3" IDLE comes pre-installed with Python and you can use this to code your Python projects

Installing PyCharm on Mac

I use PyCharm to edit, run and debug my Python code. It does not exist in the App Store. Instead, get it from  https://www.jetbrains.com/pycharm/ Get the community edition for learning. The installation is about 340 mb. Once it's installed go ahead and launch the application. Before you go ahead and create a new project, change the Python Interpreter from Virtualenv Environment to System Interpreter and select the latest version of Python (in my case it's Python3 that we have just installed) Tip: Skip to about 8 minutes in to this video for more info:  https://www.youtube.com/watch?v=oyzH4M6X6F4

What's an IDE?

IDE is short for Integrated Development Enviroment. It's basically a tool / piece of software that allows you to write your code, test it and debug it. You could code from Notepad or similar but an IDE just makes things easier and quicker to execute etc. For Python there a couple of IDEs out there: Spyder PyCharm (JetBrain) <-- I use this Thonny Atom Jupytor Notebook

How to install Python on a Mac

It's easy to get started (they say!) because Python comes pre-installed on Macintosh (Mac) OS X. But you need to install the newest version, so there is a bit to do. I downloaded the  Python 3.4.0 Mac OS X 32-bit i386/PPC Installer  but got the message "The installer could not install the software because there was no software found to install".  I also tried the following version, python 2.7.6 for Mac OS X 10.3 or later for the following architecture(s) i386, ppc. , and got the same error message. Anyway, I proceeded to this site  https://www.python.org/downloads/release/python-374/  and got the  macOS 64-bit/32-bit installer  and that worked like a charm. You can confirm the install by running Terminal and type in " python3 " (maybe Python4 etc. in future versions). You're all set now. Important: Don't remove or modify the original Python that Mac comes pre-installed with. It will screw things up. Resources: https://legacy.pyt...