Пример #1
0
Файл: menu.py Проект: rmln/dtknv
    def assign_chk(self, v):
        """
        Assign value from menu to global settings.
        This is to pass values into the self.set, which
        is then saved in settings.py.
        
        I.e.:

        self.set.set_recursive = self.var_sett_recursive.get()
        
        """
        setattr(self.set, 'set_%s' % v,
                getattr(self, 'var_sett_%s' % v).get())
        if v == 'sameinout' and self.set.set_sameinout:
            # "Save in same dir" option is on, so set the paths
            if self.set.set_dir != self.set.NOP:
                self.set.set_dirout = self.set.set_dir
            # Same as above, but for a file:
            if self.set.set_file != self.set.NOP:
                self.set.set_dirout = helpers.get_file_path(self.set.set_file)
            self.master.update_gui()
        if v == 'sameinout' and not self.set.set_sameinout:
            self.set.set_dirout =  self.set.NOP
            self.master.update_gui()
        if v == 'recursive':
            self.master.update_gui()
Пример #2
0
def write_to_file(results):
    file_path = helpers.get_file_path('yelp.json', subdirectory='results')
    f = open(file_path, 'w')
    f.write(json.dumps(results))


# Challenge: Figure out how to print all of the reviews for Tomate on Noyes.
Пример #3
0
def write_to_file(html):
    file_path = helpers.get_file_path('spotify_album_artists.html', subdirectory='results')
    f = open(file_path, 'w')
    f.write(html)
    f.close()
    browser= webbrowser.get('chrome')
    absolute_path = os.path.abspath(file_path)
    browser.open('file://' + absolute_path)
Пример #4
0
def get_yelp_categories_children(parent):
    f = open(helpers.get_file_path('categories.json'), 'rb')
    data = json.loads(f.read())
    categories = []
    for record in data:
        if parent in record['parents']:
            categories.append(record['title'])
    categories.sort()
    return categories
Пример #5
0
def problem_7():
    _print_title('Problem 7')

    def get_mail_providers(file):
        providers = []
        for line in file:
            line = line.replace('\n', '')
            tokens = line.split('@')
            if len(tokens) == 1:
                continue
            providers.append(tokens[-1])
        return providers

    file_path = helpers.get_file_path('contacts.csv')
    f = open(file_path, 'r')
    print(get_mail_providers(f))
def save_urls_to_database(urls):
    db_path = helpers.get_file_path('test_database.db', subdirectory='results')
    create_table_if_does_not_exist(db_path)
    delete_all_rows_in_table(db_path, 'Images')

    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    for url in urls:
        print('Writing', url, 'to the database...')
        cur.execute(
            '''
            INSERT INTO Images (url)
            VALUES (?)''', (url, ))
    cur.close()
    conn.commit()
    conn.close()
Пример #7
0
def problem_6():
    _print_title('Problem 6')
    '''
    The errors have been corrected in this code, but the errors are:
        file_to_data: 
          1. incorrect syntax for adding a dictionary to a list. Use append not +=
          2. needs to skip the first line (because it's a header row)
          3. prints the data list instead of returning it

        generate_mailers:
          4. The string.format is treating the person object as a list,
             but it's a dictionary
    '''
    def file_to_data(file):
        data = []
        counter = 0
        for line in file:
            counter += 1
            if counter == 1:
                # skips header row:
                continue
            line = line.replace('\n', '')
            cells = line.split(',')
            data.append({
                'first_name': cells[0],
                'last_name': cells[1],
                'email': cells[2]
            })
        return data

    def generate_mailers(data):
        for person in data:
            print('''
                    To: {0}
                    Dear {1},
                    Please send us some money for our important cause.
                    -- The Nature Conservancy
                '''.format(person['email'], person['first_name']))

    file_path = helpers.get_file_path('contacts.csv')
    file = open(file_path, 'r')
    generate_mailers(file_to_data(file))
    file.close()
Пример #8
0
Файл: menu.py Проект: rmln/dtknv
 def browse_file(self, *e):
     """
     Browse for a file.
     """
     filetypes = self.get_file_descriptions()
     path  = elements.Browse(mode='file',
                             filetypes=filetypes,
                             initpath=self.set.set_last_filepath,
                             lng=self.lng).path
     if path != '':
         self.set.set_file = path
         path_to_file = helpers.get_file_path(self.set.set_file)
         self.set.set_last_filepath = path_to_file
         self.set.set_dir = self.set.NOP
         # If same/in out is selected, set the proper
         # out path:
         if self.set.set_sameinout:
             self.set.set_dirout = path_to_file
         self.master.update_gui()
         self.master.show_filesdir()
Пример #9
0
def problem_8():
    _print_title('Problem 8')

    def get_mail_groups(file):
        my_dictionary = {}
        for line in file:
            line = line.replace('\n', '')
            cells = line.split('@')
            if len(cells) == 1:
                continue
            provider = cells[1]
            email = line.split(',')[-1]
            if my_dictionary.get(provider) is None:
                my_dictionary[provider] = []
            my_dictionary[provider].append(email)
        return my_dictionary

    file_path = helpers.get_file_path('contacts.csv')
    f = open(file_path, 'r')
    print(get_mail_groups(f))
Пример #10
0
import pandas as pd
import sqlite3
import helpers

# Now let's ask a different question. How many allegations are there
# by Beat?
file_path = helpers.get_file_path('cpd.db', subdirectory='data_sources')
conn = sqlite3.connect(file_path)
df = pd.read_sql_query(
    '''
        SELECT beat_id, count(beat_id) as num_complaints
        FROM data_allegation
        GROUP BY beat_id 
        ORDER BY num_complaints desc;
    ''', conn)
pd.set_option('display.max_colwidth',
              -1)  # so the table does not get truncated.

template = '''
    <html>
        <head>
            <title>{header}</title>
            <link rel="stylesheet" href="styles.css" />
        </head>
        <body>
            <h1>{header}</h1>
            <p>{summary}</p>
            {table}
        </body>
    </html>
'''
Пример #11
0
import helpers
import pprint
import json

file_path = helpers.get_file_path('artists.json', subdirectory='data_sources')
f = open(file_path, 'r')
artists = json.load(f)
# pprint.pprint(artists)

print(artists)
'''
Challenges: 
1. Print the name of the third artist in the list
2. Print the url that is associated with the image for the second artist
3. Build a list of image urls
4. Print the name of all of the artists
'''
Пример #12
0
from urllib.request import urlopen
import helpers


# From a local file:
file_path = helpers.get_file_path('sample_file.txt', subdirectory='sample_files')
f = open(file_path, 'r')
# output it to the screen:
for line in f:
    print(line)

# From a remote file:
results = urlopen('http://www.google.com').read().decode('utf-8', 'ignore')

# 2. print it to the screen
print(results)
Пример #13
0
import helpers

some_data = [
    {
        'name': 'Sally',
        'age': 34
    },
    {
        'name': 'Larry',
        'age': 14
    },
    {
        'name': 'Vivian',
        'age': 15
    },
    {
        'name': 'Mari',
        'age': 20
    },
    {
        'name': 'Jessie',
        'age': 50
    },
]
file_path = helpers.get_file_path('names.txt', subdirectory='results')
f = open(file_path, 'w')
for item in some_data:
    f.write(item['name'] + '\n')
f.close()
Пример #14
0
def write_to_file(results):
    file_path = helpers.get_file_path('yelp.json', subdirectory='results')
    f = open(file_path, 'w')
    f.write(json.dumps(results))
    f.close()
    'header': 'This is my first page title',
    'image_url': '../sample_files/kittens.jpg',
    'summary': 'This is a very cute photo of a kitten'
}, {
    'header': 'This is my second page title',
    'image_url': '../sample_files/kittens.jpg',
    'summary': 'This is a very cute photo of a kitten'
}, {
    'header': 'This is my third page title',
    'image_url': '../sample_files/kittens.jpg',
    'summary': 'This is a very cute photo of a kitten'
}, {
    'header': 'This is my fourth page title',
    'image_url': '../sample_files/kittens.jpg',
    'summary': 'This is a very cute photo of a kitten'
}, {
    'header': 'This is my fifth page title',
    'image_url': '../sample_files/kittens.jpg',
    'summary': 'This is a very cute photo of a kitten'
}]

# generate an HTML file for each item in the list
counter = 1
for item in data:
    html_text = template.format(**item)
    file_path = helpers.get_file_path('cat_page_{0}.html'.format(counter),
                                      subdirectory='results')
    f = open(file_path, 'w')
    f.write(html_text)
    f.close()
    counter += 1
Пример #16
0
                                 color='teal',
                                 radius=available_bikes * 8,
                                 fill_color='teal',
                                 fill_opacity=0.6)
    marker.add_to(folium_map)

template = '''
    <html>
        <head>
            <title>{header}</title>
            <link rel="stylesheet" href="styles.css" />
        </head>
        <body>
            <h1>{header}</h1>
            <p>{summary}</p>
            <div style="width: 800px; margin:auto;">
                {map}
            </div>
        </body>
    </html>
'''
html_text = template.format(
    header='My Webpage Title',
    summary='Here, we can put a summary of this web page',
    map=folium_map._repr_html_())

print('generating the map file...')
file_path = helpers.get_file_path('embedded_map.html', subdirectory='results')
f = open(file_path, 'w')
f.write(html_text)
f.close()
Пример #17
0
# https://python-visualization.github.io/folium/
# https://blog.prototypr.io/interactive-maps-with-python-part-1-aa1563dbe5a9
# https://nbviewer.jupyter.org/github/vincentropy/python_cartography_tutorial/blob/master/part1_basic_folium_maps.ipynb
import folium
import json
import urllib
import helpers
import pprint

folium_map = folium.Map(
    location=[42.004583, -87.661406],
    zoom_start=13,
    tiles="Stamen watercolor"  # Switch to "Stamen toner"
)

print('generating the map file...')
file_name = 'map_no_data.html'
file_path = helpers.get_file_path(file_name, subdirectory='results')
folium_map.save(file_path)
Пример #18
0
pd.set_option('display.max_colwidth', -1) # so the table does not get truncated.

def display_image_html(entry):
    # instead of showing a dictionary, use the 'url' key to display an image:
    return '<img src="{0}" />'.format(entry['url'])

def display_genres(entry):
    # instead of showing a list, convert to a comma-separated string:
    return ', '.join(entry)

def display_link(entry):
    # instead of just showing the entry, convert the entry to a link:
    return '<a href="{0}">View on Spotify</a>'.format(entry)


file_path = helpers.get_file_path('artists.json', subdirectory='data_sources')
dataframe = pd.read_json(file_path)

# how we want each column to be displayed (see documentation):
# http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html
formatters = {
    'image': display_image_html,
    'genres': display_genres,
    'url': display_link
}

template = '''
    <html>
        <head>
            <title>{header}</title>
            <link rel="stylesheet" href="styles.css" />
import helpers

template = '''
    <html>
        <head><title>{header}</title></head>
        <body>
            <h1>{header}</h1>
            <p>{summary}</p>
            <img src="{image_url}" />
        </body>
    </html>
'''

html_text = template.format(header='This is my page title',
                            image_url='../sample_files/kittens.jpg',
                            summary='This is a very cute photo of a kitten')

file_path = helpers.get_file_path('cat_page.html', subdirectory='results')
f = open(file_path, 'w')
f.write(html_text)
f.close()
Пример #20
0
from urllib.request import urlopen
import helpers

# 1. get it from the internet
results = urlopen('http://www.google.com').read().decode('utf-8', 'ignore')

# 2. print it to the screen
print(results)


# 3. write it to a file
# create a file path that will write to results/google.html:
file_path = helpers.get_file_path('google.html', subdirectory='results')
f = open(file_path, 'w')
f.write(results)
f.close()
Пример #21
0
def write_to_file(results, file_name='yelp_data.json'):
    file_path = helpers.get_file_path(file_name, subdirectory='results')
    f = open(file_path, 'w')
    f.write(json.dumps(results))
    f.close()
Пример #22
0
    "genres": ["dance pop", "pop"],
    "id": "1HY2Jd0NmPuamShAr6KMms",
    "image": {
        "height": 640,
        "url":
        "https://i.scdn.co/image/6fdeaf010a560e24ed9fdfab939dd0ab01d3d32c",
        "width": 640
    },
    "url": "https://api.spotify.com/v1/artists/1HY2Jd0NmPuamShAr6KMms"
}

template = '''
    <html>
        <head><title>{artist}</title></head>
        <body>
            <h1>{artist}</h1>
            <p>Genres: {genres}</p>
            <img src="{image_url}" />
        </body>
    </html>
'''

# Currently the template is being populated with the wrong
# data. Fix it so that it uses the data from artists:
html_text = template.format(artist='a', genres='b', image_url='c')

# write to file:
file_path = helpers.get_file_path('lady_gaga.html', subdirectory='results')
f = open(file_path, 'w')
f.write(html_text)
f.close()
Пример #23
0
import helpers
'''
Problem 8
Write a function called get_students_by_major that takes 
a file object as an argument and returns a 
list of student names that where they key is the major 
and the value is a list of students belonging to the major.
'''


def get_students_by_major(file, student_major):
    student_names = []
    for line in file.readlines()[1:]:
        line = line.replace('\n', '')
        cells = line.split(',')
        major = cells[2]
        if student_major == major:
            full_name = cells[0] + ' ' + cells[1]
            student_names.append(full_name)
    return student_names


f = open(helpers.get_file_path('students.csv'), 'r')
student_names = get_students_by_major(f, 'Psychology')
print(student_names)
Пример #24
0
import pylab
import matplotlib.pyplot as plt
import numpy as np
import sqlite3
import helpers

# get data:
file_path = helpers.get_file_path('cpd.db', subdirectory='data_sources')
conn = sqlite3.connect(file_path)
cur = conn.cursor()
cur.execute('''
    SELECT beat_id, count(beat_id) as num_complaints
    FROM data_allegation
    GROUP BY beat_id 
    ORDER BY num_complaints
    LIMIT 25;
''')
results = cur.fetchall()
cur.close()
conn.close()

# make a list with all of the beat names
# and a list with all of the complaint counts
beats = []
counts = []
for result in results:
    # omit empty data:
    if result[0] is None or result[1] is None:
        continue
    beats.append('Beat ' + str(int(result[0])))
    counts.append(result[1])
import pylab
import matplotlib.pyplot as plt
import numpy as np
import sqlite3
import helpers

# How do we display the same query as a bar chart?
# Answer: use pylab and matplotlib
file_path = helpers.get_file_path('cpd.db', subdirectory='data_sources')
conn = sqlite3.connect(file_path)
cur = conn.cursor()
cur.execute('''
    SELECT beat_id, count(beat_id) as num_complaints
    FROM data_allegation
    GROUP BY beat_id 
    ORDER BY num_complaints
    LIMIT 25;
''')
results = cur.fetchall()
cur.close()
conn.close()

# make a list with all of the beat names
# and a list with all of the complaint counts
beats = []
counts = []
for result in results:
    # omit empty data:
    if result[0] is None or result[1] is None:
        continue
    beats.append('Beat ' + str(int(result[0])))
Пример #26
0
                    font-family: Arial; 
                    margin: 30px 100px 30px 100px;
                }}
                table {{ margin-top: 20px;}}
            </style>
        </head>
        <body>
            <h1>{artist_name}</h1>
            <p>More info <a href="{share_url}">here...</a></p>
            <img src="{image_url}" />
            {tracks_table}
        </body>
    </html>
'''

artist_data = spotify.get_artists('beyonce')
artist = artist_data[0]
tracks_data = spotify.get_tracks_by_artist(artist['id'])
tracks_table = spotify.get_formatted_tracklist_table_html(tracks_data)

# print(artist)
# print(tracks_data)
html_text = template.format(artist_name=artist['name'],
                            image_url=artist['image_url_small'],
                            share_url='share_url',
                            tracks_table=tracks_table)

file_path = helpers.get_file_path('beyonce.html', subdirectory='results')
f = open(file_path, 'w')
f.write(html_text)
f.close()
Пример #27
0
# inject results into simple HTML page:
template = '''
    <html>
        <head>
            <title>Northwestern Search Results</title>
        </head>
        <body>
            <h1>Search Results</h1>
            {body}
        </body>
    </html>
'''.format(body=body_section)

# write everything to a file:
file_path = helpers.get_file_path(search_term.replace(' ', '_') + '.html',
                                  subdirectory='results')
absolute_path = os.path.abspath(file_path)

f = open(file_path, 'w')
f.write(template)
f.close()

print('Copy this link into your web browser to see the results:')
print('file://' + absolute_path)
try:
    # open web browser with the results:
    browser = webbrowser.get('chrome')
    browser.open('file://' + absolute_path)
except:
    print('Your web page did not open automatically.')
Пример #28
0
def write_to_file(data):
    file_path = helpers.get_file_path('spotify_artists.json', subdirectory='results')
    f = open(file_path, 'w')
    f.write(json.dumps(data))
import sqlite3
import helpers

conn = sqlite3.connect(helpers.get_file_path('../databases/flights.db'))
cur = conn.cursor()

# list columns for each of the available tables:
for table in ['airports', 'airlines', 'routes']:
    cur.execute("pragma table_info({0});".format(table))
    results = cur.fetchall()
    # add a little formatting to make things easier to read:
    print('\n')
    print(table)
    print('-' * 35)
    for row in results:
        print(row[1] + ' (' +  row[2] + ')')

cur.close()
conn.close()
Пример #30
0
import helpers

template = '''
    <html>
        <head><title>{header}</title></head>
        <body>
            <h1>{header}</h1>
            <p>{summary}</p>
            <img src="{image_url}" />
        </body>
    </html>
'''

html_text = template.format(
    header='This is my page title',
    image_url='../sample_files/kittens.jpg',
    summary='This is a very cute photo of a kitten'
)

file_path = helpers.get_file_path('cat_page.html')
f = open(file_path, 'w')
f.write(html_text)
f.close()