snippsat 661 Master Poster

The same for me "InBox" and "Send Message" has not work after change to Dazah.
Both give Rate limit exceeded: Please try your request again in a few minutes..
Same on all pc.

Has to copy cookie to from working login,to other computer just to get login to work.
So not been happy with this Dazah change,have not been active here for a while to.

snippsat 661 Master Poster

Use wheel as posted bye Gribouillis.
PyMongo has C depencies so to avoid this Installing from source on Windows
Go here,when dowloaded your version.
Do e.g pip install pymongo-3.3.0-cp35-cp35m-win32.whl
Wheel(.whl) has all C depencies packed,no need to compile.

snippsat 661 Master Poster

It's part of standar library,so it should work.
Here a run in Python 3.4.

>>> from http import cookies
>>> a_cooike = cookies.SimpleCookie()
>>> a_cooike['Base'] = 'cream'
>>> print(a_cooike)
Set-Cookie: Base=cream

The default placement for python 3.5 is terrible,what are developers thinking about?
C:\Users\hohait\AppData\Local\Programs\Python\Python35-32
I know the answer and it security relatet,but i don't like it.

Follow this,the you get better placement C:\Python35-32
Make sure under installation that you mark for Path and pip.
Test pip and upgrade pip.
Here a run and use virtualenv,so you see how pip work from clean setup.

C:\test\Scripts
λ activate

(test) C:\test\Scripts
λ python -m pip install --upgrade pip
Requirement already up-to-date: pip in c:\test\lib\site-packages

(test) C:\test\Scripts
λ pip -V
pip 8.1.2 from c:\test\lib\site-packages (python 3.4)

(test) C:\test\Scripts
λ pip install requests
Collecting requests
  Downloading requests-2.11.0-py2.py3-none-any.whl (514kB)
    100% |################################| 522kB 1.0MB/s
Installing collected packages: requests
Successfully installed requests-2.11.0

(test) C:\test\Scripts
λ python
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.11.0'

#And also test cookies
>>> from http import cookies
>>>
snippsat 661 Master Poster

It's in http.cookie for Python 3.x.
So import is from http import cookies

For all about HTTP use Requests

>>> import requests
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies='are good to eat')
>>> r = requests.get(url, cookies=cookies)
>>> print(r.text)
{
  "cookies": {
    "cookies": "are good to eat"
  }
}
snippsat 661 Master Poster

I like python and I wanted to give it a try at web programming but the setup and management is too difficult. I'll just go back to php.

It's not so difficult,and you do not need stuff like LAMP.
Django and Flask has build in web-server this you use for all development local.

Try Flask is as easy as it get,try hello world example on the front page.
Run the 7 lines hello.py then it's a server,and you go to http://localhost:5000/ in browser to see result.

I use Flask for all my web-development,the simplicity and power it has to build all from small to bigger web apps is great.
Just a example of an simple image viewer,
i just iterate over some of my images and use CSS to organize little.
And i delpoy to web,you can see result here Image viewer

So it's like 40 lines of code and never used LAMP or any other stuff,just the build in web-server.
I talk more deploy and this stuff in this post.

server.py

from flask import Flask, render_template, jsonify
import os

app = Flask(__name__,static_url_path='')    
@app.route('/')
def img():
    pics = os.listdir('mysite/static/images')
    return render_template('img1.html', pics=pics)

if __name__ == '__main__':
  app.run()

index.html

 <!DOCTYPE html>
<html>
<head>
  <style>
    body {
      background: radial-gradient(#E0D9C6, #4B3A40);
    }
    h1 {
      text-align: center;
      font-weight: bold;
      color: #663333;
      font-size:60px;
    }
    .thumbnail {
      float: left;
      width: auto;
      margin: 2% 2% 30px 7%;
      height: 600px;
    }
  </style>
</head>
  <body>
     <h1>Image viewer</h1> …
snippsat 661 Master Poster

what if I need to take seperate nosetests html report for each file.. How can I do that ?

Yes as bash script looping over files will work,or a Python script with subprocess.
There are build into nose to test all files that start test.
So naming your files test_a.py test_b.py... will work.

But this give you on report at end,
there are soultion like tappy

I use pytest,not tested but this should work,
py.test --files --html=report.html test all file in a folder,
and each files get own report.

snippsat 661 Master Poster

The problem can be solved before give data to Pandas.
As mention over there are lot of cases to think about.
I would have made up rules with regex.
Example.

 >>> import re

>>> s = "Richard Wayne Van Dyke"
>>> re.split(r"\s*(?:(?:[a-zA-Z]')|(Van \w.*))", s, re.I)[:-1]
['Richard Wayne', 'Van Dyke']
>>> s = 'Wayne Van Dyke'
>>> re.split(r"\s*(?:(?:[a-zA-Z]')|(Van \w.*))", s, re.I)[:-1]
['Wayne', 'Van Dyke']
>>> s = 'Edda Kathleen Van Heemstra Hepburn-Ruston'
>>> re.split(r"\s*(?:(?:[a-zA-Z]')|(Van \w.*))", s, re.I)[:-1]
['Edda Kathleen', 'Van Heemstra Hepburn-Ruston']

>>> s = "Gary Del Barco"
>>> re.split(r"\s*(?:(?:[a-zA-Z]')|(Del \w.*))", s, re.I)[:-1]
['Gary', 'Del Barco']

>>> s = 'Dave Allen Smith'
>>> re.split(r"\s*(?:(?:[a-zA-Z]')|(Del \w.*))", s, re.I)[:-1]
[]

So key word like Van and Del,in this regex will take key word and all word after as last name.
If need more rule than this,you have to make own make rule for problem names.
If pass in name that's not in regex,a empy list is returned.

mattrweaver commented: Thanks for the responses. I will be working with a limited set of data periodically, so I can easily write new rules for problem names. +0
snippsat 661 Master Poster

I've seen the Full stack python, it also gives you many options to choose & but not how do I choose them

Yes it can be confusing,Nginx + Gunicorn,Tornado,uWSGI?
Here is little info about Heroku and PythonAnywhere.

@Heroku
Do Python applications run behind Nginx?
So on Heroku you do not use Nginx,you just have to setup a server like Gunicorn.
You see how in Getting Started with Python on Heroku

PythonAnywhere is very easy,and do like all deploy stuff for you.
You use UI and upload files and do a coulpe of config choices.
Then you have website,and PythonAnywhere run that site with,
Nginx to do routing and uWSGI to run the apps.

snippsat 661 Master Poster

In the first question, ctually asking which directory on linux should I choose to place a project? Like /home/user/ or /var/www etc?

Where I should put my python scripts in Linux?.

In the 3rd question, you said you'd use something like Gunicorn,Tornado,uWSGI or Apache. The question how do you decide between so many options. When do you want to use Gunicorn & when do you prefer mod_wsgi on Apache like that?

I like Gunicorn easy to setup,and default options works good for most cases.
I you search eg: Gunicorn vs uWSGI you get a lot of opinions.

Look at Full Stack Python,there is a lot of info on deploy and run a Python web application.
For smaller personal projects you don't have to think to much about the deploy side.
For bigger commercial sites there is a lot of tought/work on the deploy side.

Gribouillis commented: awesome +0
snippsat 661 Master Poster

Where should I host a python project on linux? I'm sure there'd be multiple choices. So, Where would you host it & on what circumstances?

Github,Bitbucket...
If you mean web-host,pythonanywhere,Heroku,Webfaction,Digital ocean...

I understand that virtualenv creates a virtual environment separate from others. But, why should one need it? Do we use it in production too, if so, why?

You pip install stuff into virtualenv,
so you always get newest versions of modules/package.
It's isolatet,so it don't mess with your OS version of Python.
As you see in this Getting Started tutorial is normal to use virtualenv when using a web-host.

If I want to create a web application using python? What server should I choose & on what basis should I choose them for a given project.

Flask and Django has build in webserver,
this is good for working/testing local and building a web application.

When finish and want to deploy to web,
then think of(shall i use Gunicorn,Tornado,uWSGI or running mod_wsgi (Apache)?
Host like Pythonanywhere,Heroku make it easier to deploy Python code.

Gribouillis commented: great answer +14
snippsat 661 Master Poster

If you search for cola you see this url.
https://datacvr.virk.dk/data/visninger?soeg=cola&type=Alle
Generate own search url's.

>>> url = 'https://datacvr.virk.dk/data/visninger?soeg={}&type=Alle'
>>> search_lst = ['car', 'train']
>>> for item in search_lst:
...     print(url.format(item))
...     
https://datacvr.virk.dk/data/visninger?soeg=car&type=Alle
https://datacvr.virk.dk/data/visninger?soeg=train&type=Alle

E.g of take out some data,here all title text when search for cola.

import requests
from bs4 import BeautifulSoup

url = 'https://datacvr.virk.dk/data/visninger?soeg=cola&type=Alle'
page = requests.get(url)
soup = BeautifulSoup(page.content)
tile_tag = soup.find_all('h2', {'class': 'name'})
for name in tile_tag:
    print(name.text.strip())

"""Output-->
COCA-COLA NORDIC SERVICES ApS
Cola klubben
Coca-Cola Service S.A.
The Coca-Cola Company
CARLSBERG DANMARK A/S
Abby Thai Kropsmassage v/Ornanong Johansen
Karim Rahima
Coca-Cola Service S.A.
CARLSBERG DANMARK A/S Coca -Cola Tapperierne
COCA-COLA NORDIC SERVICES ApS
"""
Slavi commented: Thank you +6
snippsat 661 Master Poster

Here is a hint.

>>> user_input = 'The quick brown fox jumps over a lazy dog'
>>> word_lst = user_input.split()
>>> word_lst
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'a', 'lazy', 'dog']
>>> len(word_lst)
9
>>> [len(c) for c in word_lst]
[3, 5, 5, 3, 5, 4, 1, 4, 3]
sum([len(c) for c in word_lst])
33

If you wonder about [len(c) for c in word_lst],so is this called list comprehensions.
list comprehensions is used very much in Python.
To write it in a ordinary loop, it looks like this.

>>> char_count = []
>>> for c in word_lst:
...     char_count.append(len(c))
...     
>>> char_count
[3, 5, 5, 3, 5, 4, 1, 4, 3]
snippsat 661 Master Poster

snippsat I understand the inputting it into a gui or >>> but I need it to take any user input and not just set filename. Unless I set filename to equal a list of file extensions.

from os.path import splitext

def parse_extension(filename): 
    if filename == splitext(filename)[-1]:
        print 'Please Enter a FileName: ' #Think of what you are doing here
    elif filename != splitext(filename)[-1]:
        print splitext(filename)[-1]

filename = raw_input('Please Enter a File Name: ')
parse_extension(filename)

I dont understand how the import os feature works

os is part of Python standard library,just import os and use it.
Doc Miscellaneous operating system interfaces
Tutorial OS Module

snippsat 661 Master Poster

wudie47 read your post,just 1 day old,
vegaseat did answer with the preferred way os.path.splitext().

>>> import os
>>> filename = 'python.exe'
>>> os.path.splitext(filename)
('python', '.exe')
>>> os.path.splitext(filename)[-1]
'.exe'
>>> name, extension = os.path.splitext(filename)
>>> name
'python'
>>> extension
'.exe'
snippsat 661 Master Poster

I installed Python v 3.4.3

pip comes pre-installed on Python 3.4.3,do not install pip.
First set up environment Variables Path,
Same as in video,but also point to script folder(pip placement),so for Python 3.4 you add ;C:\python34\;C:\python34\scripts to Path.

What "terminal" am I using, and what's with the "$" symbol? I dunno'.
I tried various lines in a CMD window, but nothing worked.

The "$" is for Linux,you use cmd.
Try again in cmd now that you have set environment Variables.
pip install requests

lxml-3.4.4.win32-py3.2.exe
I then started it, and it said...
"Python version 3.2 required, which was not found in the registry."

For lxml go to Gohlke
Download lxml‑3.4.4‑cp33‑none‑win32.whl to e.g python34/scripts folder.
Navigate in cmd to scripts folder,then do pip install lxml‑3.4.4‑cp33‑none‑win32.whl
Test that it work.

>>> from lxml import etree
>>> etree.__version__
u'3.3.0'
snippsat 661 Master Poster

It's a little old way of doing this,but it work if fixing as Girb posted.
You should not make a seperate line for count.
Do it like this.

my_dict = {}
s = 'hello world hello'
for word in s.split():
    if word in my_dict:
        my_dict[word] += 1
    else:
        my_dict[word] = 1

print(my_dict) #{'world': 1, 'hello': 2}

The next step in improving this,can be to use get() method.

my_dict = {}
s = 'hello world hello'
for word in s.split():
    my_dict[word] = my_dict.get(word, 0) + 1

print(my_dict) #{'world': 1, 'hello': 2}

Your code with full Python power,here i use colletions.Counter.
And regex to remove punctuation.
You should also look at PEP-8,e.g not use capitale letter in variable name.

from collections import Counter
import re    

with open('count_test.txt') as f:
    text = f.read().lower()
words = re.findall(r'\w+', text)

print(Counter(words))
print(Counter(words).most_common(2))
Slavi commented: love the get() +6
snippsat 661 Master Poster

Done a little more testing.
Here from wheel doc

Many packages will be properly installed with only the “Unpack” step (simply extracting the file onto sys.path), and the unpacked archive preserves enough information to “Spread” (copy data and scripts to their final locations) at any later time.

Here test with lxml,i use virtualenv so i am sure OS is not affected.

from zipfile import ZipFile

#This get extractet to sys.path
with ZipFile('lxml-3.4.2-cp34-none-win32') as myzip:
    myzip.extractall()

Test:

(test) C:\Documents and Settings\Tom\Envs\test\Scripts
λ python
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
>>> from lxml.html import parse,etree
>>> etree.__version__
u'3.4.4'

So with lxml it works fine.

The same with PyQt4 wheel dos not work,i got it almost to work.
I moved folder PyQt4 in purelib folder to sys.path.
Now get "ImportError: No module named sip" should be possible to fix.

snippsat 661 Master Poster

no, I've built an exe using GameMaker8 (has an icon and loading banner) which >boots Portable Python with python.exe loader.py

As usually i think you are doing some strange stuff:)
So you are talking about Portable Python 2.7.6.1
Which has PyQt4 installed,so why do you need to install PyQt4?
Maybe you should explain better,for i think not many will follow this stuff.

EDIT:
btw, the latest Portable Python is 2.7.6.1
would be nice if there was a more up-to-date build

oh it looks like I have installed 2.7.9... oops
lol

normally I install the non-portable python to match the portable python. :P

I have used portable python for many years on different computers,
Portable python closed environment,so i dont know why you want it to match the ordinary Python installation?
I would not use portable python to share code i have made,this it not what's it made for.

so ultimately (now that I know it's not using the wheel on linux, but a >locally installed pyqt4) it looks like this wheel is broken.

this question will be marked solved when I have a working portable >installation.
(I will note this if I find it myself)

I downloaded the current wheel from here:
http://www.lfd.uci.edu/~gohlke/pythonlibs/

No it's not broken,i have tested that PyQt4 wheel in Virtualenv.
I have used a lot wheel(before all was exe)from Gohlke and stuff has …

snippsat 661 Master Poster

I got the wheel so I wouldn't have to install

Wheel has to installed,also same with older format egg.

(you don't need to install python to run my (not compiled) programs)

You are looking for module/package that can create standalone executables from Python scripts.
Py2exe, cx_Freeze(cross platform), Pynsist, Nuitka

I use Py2exe for my wx_nrk prosject,
has ca 2000 Windows users and ca 300-400 Linux users,
and Windows users do not have to install Python or wxPython,just click on a exe to get it to work.

so what exactly is the issue, and why do I need PyQt4 installed in this >program's directory?

It's not installing into script folder,it's installing like any other module/package you install in Python(into site-packages folder)
The wheel file from scripts folder can be deleted after installing.

I just said you should place it in scripts folder because pip is there in Python 2.7.9.
If you have set up environment variables Path to point to Python27 and Python27/Scripts folder.
Then you do pip install wheel from whatever place you choose.

snippsat 661 Master Poster

You have to install wheel with pip.
On Python 2.7.9 is pip pre-installed.
Put PyQt4-4.11.3-cp27-none-win32.whl in C:\Python27\scripts folder.
Start cmd navigate to C:\Python27\scripts folder,
then do: pip install PyQt4-4.11.3-cp27-none-win32.whl

snippsat 661 Master Poster

You use subprocess module.

import subprocess

calc_program = "path_to_program"
subprocess.call([clac_program])
snippsat 661 Master Poster

Use string formatting

>>> value = 1000000000000
>>> n = "{:,}".format(value)
>>> print(n)
1,000,000,000,000
>>> #Back to float
>>> float(n.translate(None, ','))
1000000000000.0
snippsat 661 Master Poster

psutil is good.
Here a run on my windows and Mint(VirtualBox)

>>> import psutil
>>> psutil.disk_partitions()
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'),
 sdiskpart(device='D:\\', mountpoint='D:\\', fstype='', opts='cdrom'),
 sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed'),
 sdiskpart(device='F:\\', mountpoint='F:\\', fstype='UDF', opts='ro,cdrom'),
 sdiskpart(device='G:\\', mountpoint='G:\\', fstype='NTFS', opts='rw,fixed')]
>>> psutil.disk_usage('C:\\')
sdiskusage(total=255953203200L, used=177126027264L, free=78827175936L, percent=69.2)

Mint:

>>> import psutil
>>> psutil.disk_partitions()
[partition(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro')]
>>> psutil.disk_io_counters()
iostat(read_count=32177, write_count=6804, read_bytes=833645568, write_bytes=317702144, read_time=123880, write_time=124128)
snippsat 661 Master Poster

print link['title'] what is title? The alt of the <img> tag?

Have you looked at Firebug or Chrome DevTools?
Links i gave you in post.
Then is easy to so see what <title> of the <img> is.

except KeyError: what does keyError mean here? What the keyword keyError is for?

Not all <img> on this page has a title tag,so it trow a keyError.
This is source code that gets called in BeautifulSoup.

def __getitem__(self, key):
    """tag[key] returns the value of the 'key' attribute for the tag,
       and throws an exception if it's not there."""
    return self.attrs[key]

With except KeyError: pass,just ignore those images.
Can be more specific with class="col-md-6,
so it only search for names on images we need.
Then it will not trow an error.

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.thefamouspeople.com/singers.php'
html = urllib2.urlopen(url) #Don not use read()
soup = BeautifulSoup(html)
tag_row = soup.find_all('div', {'class':'col-md-6'})
for item in tag_row:
    print item.find('img')['title']
snippsat 661 Master Poster

David W it's about time to learn string formatting :)
Splitting up with , and + something + is not so nice.

print('"{}".istitle() is {}'.format(item, item.istitle()))

Python String Format Cookbook

string formatting specifications(Gribouillis)

snippsat 661 Master Poster

So now i want my script to find every word that start with "A" in that url page >and print it for me. Then i should find a way to ask my crawle just save those >words starting with "A" that are singers names.
Very difficult!!!

That would be nightmare,and you would have to clean up a lot of rubbish text.
This is just one word that start with A Ajax.Request(,
that i saw when i quickly looked at source you get from that page.

You have to find a tag that give you info you want.
so tag <img> with <title>,will give you a fine list.

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.thefamouspeople.com/singers.php'
html = urllib2.urlopen(url) #Do not use read()
soup = BeautifulSoup(html)
link_a = soup.find_all('img')
for link in link_a:
    try:
        print link['title']
    except KeyError:
        pass

"""Ouptput--> here just 3 names befor it changes to B
Ashlee Simpson
Avril Ramona Lavigne
Axl Rose
Barbra Streisand
Barry Manilow
Barry White
"""
snippsat 661 Master Poster

he's gone too far=D

Yes of course,to make it great humoristic read.
Regex can be ok to use some times,like you only need a singel text/value.

Both BeautifulSoup and lxml has build in support for regex.
Sometime it ok to use regex as helper to parser,when parsing dynamic web-sites
you can get a at lot of rubbish text.

snippsat 661 Master Poster

Use regular expressions Click Here

No no no just to make it clear :)
Have to post this link again.
Use a parser Beautifulsoup or lxml.

from bs4 import BeautifulSoup

html = '''\
<head>
  <title>Page Title</title>
</head>
<body>
  <li>Text in li 1</li>
  <li>Text in li 2</li>
</body>
</html>'''

soup = BeautifulSoup(html)
tag_li = soup.find_all('li')
print tag_li
for tag in tag_li:
    print tag.text

"""Output-->
[<li>Text in li 1</li>, <li>Text in li 2</li>]
Text in li 1
Text in li 2
"""
Slavi commented: Great read, but this 'Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins.. he's gone too far=D +6
snippsat 661 Master Poster

so why it didn't print the url of each website into output?!

Because this is a dynamic site using JavaScript,jQuery....
The problem is that JavaScript get evaluatet by DOM in browser.
To get links we need something that automates browsers like Selenium.

Can show you one way,here i also use PhantomJS,for not loading a browser.

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='C:/phantom/phantomjs')
driver.set_window_size(1120, 550)
driver.get('https://duckduckgo.com/?q=3D&t=canonical&ia=meanings')
page_source = driver.page_source
soup = BeautifulSoup(page_source)
link_a = soup.find_all('a')
for link in set(link_a):
    if 'http' in repr(link):
         try:
            print link['href']
         except KeyError:
            pass

Output: here first 6 links of out 100 links.

http://3d.si.edu/
https://en.wikipedia.org/wiki/3-D_film
http://3d.about.com/
http://www.boxofficemojo.com/genres/chart/?id=3d.htm
http://www.urbanfonts.com/fonts/3d-fonts.htm
http://www.3dcontentcentral.com/

This is more advanced web-scraping,
and you need to study(understand) site and know what tools to use.

Gribouillis commented: very good help +14
snippsat 661 Master Poster

Change last part to this.

lst = []
for k in d:
    high = int(max(d[k]))
    lst.append((k, high))

score_lst = sorted(lst, key=lambda tup: tup[1], reverse=True)
for name,score in score_lst:
    print('{} {}'.format(name, score))

It's ok to throw away lambda,and use itemgetter as shown by slavi.

snippsat 661 Master Poster

As Andrae posted,but need to close file object.

f = open('url.txt', 'w') #Opens the file to write text
f.write(url) #Writes the file to the opened text file
f.close()

Or the best approch is to use with open(),no need to close file object.

import urllib

url = 'http://www.python.org'
text = urllib.urlopen(url).read()
with open('url.txt', 'w') as f:
    f.write(text)
snippsat 661 Master Poster
>>> answer = ('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L') 
>>> print(' '.join(answer))
7 Q 5 H 9 4 3 8 L
>>> help(''.join)
Help on built-in function join:

join(...) method of builtins.str instance
    S.join(iterable) -> str

    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.
snippsat 661 Master Poster

Hello world on the wall?
This is a rewite of some code i did for a "99 bottles of beer" challenge.
Use Google image API and take out some random images.
Use IPython Notebook to show result.
Here some result.
Hello world
Hello World Python
Hello World Linux

snippsat 661 Master Poster

Don't call both function main,use name that make sense.

import random

def generate_numbers():
    one = 1
    thirteen = 13
    subtract = 1
    number_gen = open('numbers.txt', 'w')
    for number in range(one,thirteen,subtract):
        numbers = random.randint(1,100)
        number_gen.write(str(numbers) + ' ')
    number_gen.close()

def read_numers(numb):
    numbers_read = open(numb)
    numbers = [float(n) for n in numbers_read.read().split()]
    numbers_read.close()
    for n in numbers:
        print(n)

generate_numbers()
numb = 'numbers.txt'
read_numers(numb)

A more pythonic approch.

def generate_numbers():
    with open('numbers.txt', 'w') as f_out:
        f_out.write(' '.join(str(random.randint(1,100)) for i in range(12)))

def read_numers(numb):
    with open(numb) as f:
        numbers = [float(n) for n in f.read().split()]
        for n in numbers:
            print(n)

generate_numbers()
numb = 'numbers.txt'
read_numers(numb)
snippsat 661 Master Poster

Look's like you need Avbin
An other option is to use your favorite player.

import subprocess

# pick an external mp3 player you have
sound_program = "path to player"
# pick a sound file you have
sound_file = "path to mp3"
subprocess.call([sound_program, sound_file])
snippsat 661 Master Poster

for sses in assesses, it output me 1 instead 2! What is the secret?

In your first post you dont't have "assesses" and "sses" test.
str.count() don't count overlapping occurrences.
So if you need to count overlapping occurrences you can not use str.count().
Help and doc do mention that it return non-overlapping occurrences.

>> help(str.count)
Help on method_descriptor:

count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are interpreted
    as in slice notation.

To fix my regex soultion,to count overlapping occurrences.

>>> import re
>>> text = "assesses"
>>> sub = "sses"
>>> len(re.findall(r'(?={})'.format(sub), text))
2
snippsat 661 Master Poster

can I make?:

I think it should fine,i have not read your assignment yet.
Here a test run.

#Python 3.4.2 
>>> text = input('Enter some text: ')
Enter some text: trans panamanian bananas
>>> subtext = input('Enter subtext to find: ')
Enter subtext to find: an
>>> text.count(subtext)
6

>>> text = input('Enter some text: ')
Enter some text: assessement
>>> subtext = input('Enter subtext to find: ')
Enter subtext to find: sse
>>> text.count(subtext)
2
snippsat 661 Master Poster

@snippsat I like the concept, although being based on top of sqlalchemy may >have a performance cost for dataset.

Yes,i agree there may be some performance cost,
but for many small project where you just want a easy way to get data in and out,i think it can be ok.

With a little more work, freezefiles could be used to export pickle files >instead of json or csv, making them trivial to load from python.

Yes i was looking a little into this freezefiles option.
I like json much more than pickle,and json is in the standard library.
So no need to mix in pickle.

Here is a test with freezefiles.
So here i make json file Score_table.json.

result = db['Score_table'].all()
dataset.freeze(result, format='json', filename='Score_table.json')

Load Score_table.json out,and exctact som data.

import json

with open("Score_table.json") as j:
    score_table = json.load(j)

Test:

>>> score_table
{u'count': -1,
 u'meta': {},
 u'results': [{u'id': 1, u'name': u'Tom', u'score': 250},
              {u'id': 2, u'name': u'Kent', u'score': 150},
              {u'id': 3, u'name': u'Paul', u'score': 500}]}

>>> score_table['results'][0]
{u'id': 1, u'name': u'Tom', u'score': 250}
>>> score_table['results'][1].keys()
[u'score', u'id', u'name']
>>> score_table['results'][1].values()
[150, 2, u'Kent']

>>> print('{} has the highest score of: {}'.format(score_table['results'][2]['name'], score_table['results'][2]['score']))
Paul has the highest score of: 500

export one JSON file per user
You can create one file per row by setting mode to “item”:

Test this out.

result = db['Score_table'].all()
dataset.freeze(result, format='json', filename='users/{{name}}.json', mode='item')

So this create a …

snippsat 661 Master Poster

A test with dataset,database as simple as it get.
So here i have a player list that i store in a Sqlite(comes with Python) database.

import dataset

player_lst = [
    {'name': 'Tom', 'score': 250},
    {'name': 'Kent', 'score': 150},
    {'name': 'Paul', 'score': 500}
    ]

db = dataset.connect('sqlite:///mydatabase.db')
table = db['Score_table']
for item in player_lst:
    table.insert(item)

Test it out,by taking out some data.

>>> users = db['Score_table'].all()
>>> for user in db['Score_table']:
...     print user
...     
OrderedDict([(u'id', 1), (u'score', 250), (u'name', u'Tom')])
OrderedDict([(u'id', 2), (u'score', 150), (u'name', u'Kent')])
OrderedDict([(u'id', 3), (u'score', 500), (u'name', u'Paul')])

>>> table.find_one(name='Tom')
OrderedDict([(u'id', 1), (u'score', 250), (u'name', u'Tom')])
>>> table.find_one(name='Tom')['score']
250

It also have all the power of SQL queries if needed.

>>> [i for i in db.query("SELECT MAX(score) FROM Score_table")]
[OrderedDict([(u'MAX(score)', 500)])]
>>> [i for i in db.query("SELECT MAX(score) FROM Score_table")][0]['MAX(score)']
500
Gribouillis commented: cool link +14
snippsat 661 Master Poster
>>> text = "trans panamanian bananas"
>>> text.count('an')
6

--

>>> import re
>>> len(re.findall(r'an', text))
6
snippsat 661 Master Poster

this is the HTML line which returns as soup - I'm after the 1.41 only - which
I hope to return as valueTable

Using .next_sibling can be better.

from bs4 import BeautifulSoup

html = '''\
<td class="yfnc_tablehead1" width="74%">Price/Book (mrq):</td><td class="yfnc_tabledata1">1.41</td>'''

soup = BeautifulSoup(html)
tag = soup.find('td', {'class': 'yfnc_tablehead1'})

Test with parent and nextSibling.

>>> tag
<td class="yfnc_tablehead1" width="74%">Price/Book (mrq):</td>
>>> tag.parent
<td class="yfnc_tablehead1" width="74%">Price/Book (mrq):</td><td class="yfnc_tabledata1">1.41</td>
>>> tag.parent.text
'Price/Book (mrq):1.41'    

>>> tag.nextSibling
<td class="yfnc_tabledata1">1.41</td>
>>> tag.nextSibling.text
'1.41'
>>> float(tag.nextSibling.text) + 1
2.41
snippsat 661 Master Poster

You should use after
Tkinter is running an infinite loop(the event loop).
When you use a while loop and time sleep,then this can lock up(block GUI).

That's why all GUI toolkit has some kind of timer/schedule/thread task,
that dont interfer with the running event loop.

In Wxpython that i have used most,there is wx.Timer, wx.CallLater.
Here is an example i found with Tkinter,that use this method.

import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="", width=10)
        self.label.pack()
        self.remaining = 0
        self.countdown(10)

    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining    
        if self.remaining <= 0:
            self.label.configure(text="time's up!")
        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()
snippsat 661 Master Poster

the "Price Book or class="yfnc_tabledata1" is in the return respData which is >the source code downloaded from yahoo.ca.

Ok i understand,it's just that i cant find it if search through "respData" or url.

snippsat 661 Master Poster

Cant find what you search in respData.
Do post also post your import.

import urllib.request, urllib.parse

This is the adress,you get data from.

>>> resp.geturl()
'https://ca.finance.yahoo.com/lookup?s=basics'

Do you find Price Book or class="yfnc_tabledata1 in url or in return respData?

Some notes this use JavaScript heavy,and are not a easy site to start with.
Which mean that you may have to use other method than urllib to read site.
I use Selenium to read sites like this.
Then i get executed JavaSript to,and can parse with Beautiful Soup or lxml.

Regex to parse HTML can be a bad choice,
it can work in some cases,but use a parser(BeautifulSoup) is the first choice.
I usually post this link,why not to use regex.

Gribouillis commented: good tips +14
snippsat 661 Master Poster

Pillow fork dos more than just bugfix of orginal PIL.
It now comes with new stuff and serval improvements.
So it can be smart to get Pillow instead of PIL.

snippsat 661 Master Poster

Is it possible we have both code in the one?For future GUI.

That should not be a problem,if i understand you right.

snippsat 661 Master Poster

You can not open 2 exe in binary mode,and try to merge these into 1 file.
exe are standalone programs,that has to be open separately.

Here a demo of what i mean,with subprocess and threads to open 2 files.

#2_exe_start.py
import subprocess
import threading

def file_1():
    subprocess.call([r'C:\Windows\notepad.exe'])

def file_2():
    subprocess.call([r'C:\Windows\explorer.exe'])

f1 = threading.Thread(target=file_1)
f2 = threading.Thread(target=file_2)
f1.start()
f2.start()

So this open both notepad and explorer at once.
Then i can make a exe,to open these 2 files.
So here a make 2_exe_start.exe,that's open notepad and explorer.

#make_exe.py
from distutils.core import setup
import py2exe
import sys

def py2_exe(file_in, ico):
    dest_ico = ico.split('.')[0]
    if len(sys.argv) == 1:
        sys.argv.append('py2exe')

    # Py2exe finds most module,here you can include,exclude moduls
    includes = []
    excludes = []
    packages = []
    dll_excludes = []

    # Bundle_files : 3 most stable | bundle_files : 1 create 1 big exe
    setup(options =\
    {'py2exe': {'compressed': 1,
                'optimize': 2,
                'ascii': 0,
                'bundle_files': 1,
                'includes': includes,
                'excludes': excludes,
                'packages': packages,
                'dll_excludes': dll_excludes,}},
                 zipfile = None,

    # Can use console(Command line) or windows(GUI)
    console = [{
              'script': file_in,
              #--| Uncomment for ico
              #'icon_resources' : [(1, ico)],
              #'dest_base' : dest_ico
               }])

if __name__ == '__main__':
    #--| The .py file you want to make exe of
    file_in = '2_exe_start.py'
    #--| Ico in same folder as .py
    ico = 'your.ico'
    py2_exe(file_in, ico)
snippsat 661 Master Poster

To fix your orginal code.

def translate():
    a = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"år"}
    return a

for k, v in translate().iteritems():
    print k, v

You need to practice more on using functions.
It make no sense to take a argument a,when all you have in function is a dicionary.
a as mention is not a meaningful variable name.

Something like this for translate.

def my_dict(user_input):
    return {
    "merry": "god",
    "christmas": "jul",
    "and": "och",
    "happy": "gott",
    "new": "nytt",
    "year": "år"
     }.get(user_input, "Not in dicionary")

def translate(word):
    return my_dict(word)

Test:

>>> translate('christmas')
'jul'
>>> translate('hello')
'Not in dicionary'
>>> translate('happy')
'gott'
>>> print '{} {} til alle i Sverige'.format(translate('merry'), translate('christmas'))
god jul til alle i Sverige
snippsat 661 Master Poster

A couple of ways.

from __future__ import print_function

def generate_n_chars(n, a):
    for i in range(n):
        print(a, end='')

def generate_n_chars_1(n, a):
    result = ''
    for i in range(n):
        result += a
    return result

Test:

>>> generate_n_chars(7, 'j')
jjjjjjj
>>> print generate_n_chars_1(20, 'z')
zzzzzzzzzzzzzzzzzzzz
snippsat 661 Master Poster

With enumerate().

>>> mylist1 = [1,2,3]
>>> mylist2 = ['a','b','c']
>>> for index, item in enumerate(mylist2, 2):
...     mylist1.insert(index, item)
...     
>>> mylist1
[1, 2, 'a', 'b', 'c', 3]

Flatten list approach.

def flatten(container):
    for i in container:
        if isinstance(i, list):
            for j in flatten(i):
                yield j
        else:
            yield i

def my_code(list1, list2, index, flatten):
    list1.insert(2,index)
    return list(flatten(list1))

if __name__ == '__main__':
    list1 = [1, 2, 3]
    list2 = [2]
    index = ['a','b','c']
    print my_code(list1, list2, index, flatten)
    #--> [1, 2, 'a', 'b', 'c', 3]