[THIS POST IS IN CONSTRUCTION]

In my spare time, I will be learning about Machine Learning, Artificial Intelligence, Data Science and other topics where Python is the interpreted language of choice.
Below are some fast notes I cleaned up to share to however is interested. Disclaimer: I am not responsible of the content or how you use the content. The responsibility fall on you.

We can import modules using the import keyword.

Note: Remember to use 4 spaces for indentation and use UTF-8 encoding with your editor when saving py files.

Functions

Fetch words. Assume this is in the words.py file:

from urllib.request import urlopen

def fetchWordsFromUrl(url): 
    with urlopen(url) as content:
        words = []
        for each_line in content:
            array = each_line.split()
            for each_word in array:
                words.append(each_word)
        return words

Using words.py file with python:

>>> import words
>>> words.fetchWordsFromUrl('https://wordpress.org/plugins/about/readme.txt')

Running as Script or Repo

import sys
from urllib.request import urlopen

def fetchWordsFromUrl(url): 
    with urlopen(url) as content:
        words = []
        for each_line in content:
            array = each_line.split()
            for each_word in array:
                words.append(each_word)
        return words

if __name__ == '__main__'
    fetchWordsFromUrl(sys.argv[1])

 

Other Functions

Bytes to Int 32:

def _bytes_to_int32(b):
    return b[0] |
              b([1] << 8) |
              b([2] << 16) |
              b([3] << 24)

Int 32 to Bytes:

def _int32_to_bytes(i):
    return bytes( (i & 0xff,
                         i >> 8 & 0xff,
                         i >> 16 & 0xff,
                         i >> 24 & 0xff) )

Even or Odd:

def isEvenOrOdd(n):
    return n % 2 == 0

Square:

def square(n):
    return n * n

Note: Code Execution.  

$ python3 code.py 99

 

© 2016, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette