-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting
More file actions
431 lines (359 loc) · 13.7 KB
/
Copy pathtesting
File metadata and controls
431 lines (359 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#Imports (See imports section below for info)
import random;
import urllib;
import urllib.request;
from bs4 import BeautifulSoup;
import requests;
import time
from datetime import datetime
#Simple print statements
print("oh hai!");
print("it works?");
print("4 + 4 = ");
print((4+4));
#class
class ThisIsAClass:
#Object (int) within the class
int_age = 30;
#Make an int
aNumber = 44;
#Make a String
aWord = "word";
#Mirrors substring
#print(aWord[0:2]);
aWord = aWord[0:2];
#print stuff
print(aWord);
print(aNumber);
#Create an int
int_total = 44;
#Deletes a total
del int_total;
bob = True;
#Make a new list
list_Names = ["Pat", 55, "John", 22, True];
print(list_Names);
#Print one item from the list
print(list_Names[1]);
#Append an item to the list
list_Names.append("bobbbb");
print(list_Names);
#Erase all items in the list
list_Names[:] = [];
#Make a new int, set to the size of the list_names list
int_ListSize = list_Names.__sizeof__();
print("Size of list_Names = ");
print(int_ListSize);
#Hashmaps --> Dictionaries
hash_myDictionary = {"Pat" : 55, "John" : 22, "Larry" : True, "Gary" : "False", "Bob" : ThisIsAClass.int_age};
print(hash_myDictionary);
#Print the Keys
print(hash_myDictionary.keys());
#Print the values
print(hash_myDictionary.values());
hash_smallDictionary = {};
#Print from the class object
print(ThisIsAClass.int_age);
#If Else Statement
int_age = 27;
if(int_age >= 21 & int_age < 100):
print("Old enough to drink");
elif (int_age >= 100):
print("Super old! Have a beer on me!");
else:
print("Not old enough to drink");
#If else using Strings
string_Pat = "Pat";
string_Laura = "Laura";
if(string_Pat is "pat"):
print("Is Pat");
elif (string_Laura is "Laura"):
print("Is Laura");
else :
print("Nothing");
#For Loop
list_Foods = ["Apple", "Berry", "Banana", "Milk", "Meat", "Chicken"];
int_counter = 0;
for string_f in list_Foods:
print("Food: " + string_f);
print("Length of word (characters) == " + str(len(string_f)));
int_counter += 1;
#Print counter variable. (Need to convert to String with str() function, like toString() in Java.
print("# of loops == " + str(int_counter));
print("\nNew Section\n");
#For loop, use the range function to print out 0-9 with 10 being param
for int_x in range(10):
print("at position: " + str(int_x));
print("\nNew Section\n");
#For loop, use the range function to print out 5-10 with 5, 11 being params
for int_x in range(5, 11):
print("at position: " + str(int_x));
print("\nNew Section\n");
#While loop
int_whileCounter = 0;
while int_whileCounter < 10:
print("whileCounter = " + str(int_whileCounter));
#To print a String with an int, can use str(), but can also use:
print("WhileCounter = " , int_whileCounter);
int_whileCounter += 1;
print("\nNew Section\n");
#Nested if statement, continue, break
for int_x2 in range(101):
if(int_x2 % 5 == 0):
if(int_x2 != 0):
print("Divisible by 5");
break;
else:
print("Current Number", int_x2);
continue;
print("\nNew Section\n");
#More continue examples
list_numbersTaken = [2, 5, 12, 13, 17];
for int_x3 in range(1, 20):
if int_x3 in list_numbersTaken:
continue;
print("Available Number", int_x3);
print("\nNew Section\n");
#Functions (Defining a function begins with def)
def doubleMyAge(int_x):
return (int_x * 2);
def convertBitCoinToDollar(float_BTC):
int_amount = (float_BTC) * 527;
return int_amount;
print(convertBitCoinToDollar(2.2));
print("\nNew Section\n");
#Note, '==' is for value equality comparison and 'is' is for reference equality
#http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python
#Remember to use 'or' instead of ||
#Defining the argument within the declaration gives it a default value. Useful to prevent null pointers
def getGender(string_gender="na"):
string_gender = string_gender.lower();
if(string_gender == "m" or string_gender == "male"):
print("Male");
elif(string_gender == "f" or string_gender == "female"):
print("Female");
elif(string_gender == "na"):
print("Null passed");
else:
print("No identifiable gender passed");
getGender("f"); #Should print female pattern
getGender(); #Should print default null or na pattern
print("\nNew Section\n");
#Misc
#Don't forget, you can add an r in front to prevent multiple escape characters
string_ss = r"this\doesn'tneed\ any escapes\\n";
int_a = 7777;
#Null / None
def func1(string_Name = "test", string_test2 = "oh hai!"):
#Checking for null (or none)
if(string_test2 is None):
print("test2 is null/ none");
print("func2", int_a, string_test2);
func1();
func1(None, "nope");
print("\nNew Section\n");
#Passing in undetermined number of args
def addNumbers(*int_args):
int_total = 0;
for int_a1 in int_args:
int_total += int_a1;
return int_total;
print(addNumbers(44, 44, 12, 22, 33));
print("\nNew Section\n");
#Optional parameters must be right to left. If you make age optional by adding a default = 0, so too must the rest follow
def healthCalculator(int_age, int_applesEaten, int_cigsSmoked = 0):
int_answer = (100-int_age) + (int_applesEaten * 1.05) - (int_cigsSmoked * 1.77);
return int_answer;
list_myData = [30, 5, 0];
print(list_myData);
#Passing the list details individually items via pulling from the list (array)
print("You will live to the ripe old age of: " + str(healthCalculator(list_myData[0], list_myData[1], list_myData[2])))
#A better way to do this as it passes the entire list
print("You will live to the ripe old age of: " + str(healthCalculator(*list_myData)))
print("\nNew Section\n");
#Sets. They cannot have duplicates so at runtime, this will exclude the second milk.
#NOTE: Sets DO NOT Maintain an order, it does not maintain an order
set_groceries = {"Cereal", "Milk", "Fruit Snacks", "Popcicles", "Milk"};
print(set_groceries);
#Checking within a set
if ("Milk" in set_groceries):
print("You already have milk!");
else:
print("You don't have milk, adding it to the list");
print("\nNew Section\n");
#Dictionaries (Hashmaps). DOES NOT MAINTAIN AN ORDER, but WILL keep pair matching
dictionary_classmates = {"Pat" : "Cool guy", "Bob" : "Kind of a nerd" , "Emma" : "Cute", "Sophie" : "RUDE"};
print(dictionary_classmates);
#Loop through dictionary
for key_k, value_v in dictionary_classmates.items():
print(str(key_k), "-", str(value_v));
print("\nNew Section\n");
#Modules - These are similar to Utility methods in a separate class with a ton of methods
#Referencing UtilityMethods.py, importing it here
import UtilityMethods
UtilityMethods.m("print me!", "Please", 2, True);
UtilityMethods.mSameLine("print me!", "Please", 2, True);
print("\nNew Section\n");
#Using a lib
def downloadWebImage(string_url):
#int_randomInt = random.randrange(1, 1000);
int_randomInt = 10;
string_fullName = str(int_randomInt) + ".jpg";
urllib.request.urlretrieve(string_url, string_fullName);
#Download an image from the web using the urllib.request import. Commented out for now
string_urlToDownload = "https://yt3.ggpht.com/--n5ELY2uT-U/AAAAAAAAAAI/AAAAAAAAAAA/d9JvaIEpstw/s88-c-k-no-rj-c0xffffff/photo.jpg";
#downloadWebImage(string_urlToDownload);
print("\nNew Section\n");
#Print to a file
string_nameOfTextFile = "myText.txt";
#w for write, r for read
file_fw = open(string_nameOfTextFile, "w");
#Write the data, NOTE! this will erase and rewrite, not append
file_fw.write("Write some data here to the text file\nSound good?");
file_fw.write("\nMoar Data!");
#Don't forget to close it
file_fw.close();
#Read the data
file_fr = open(string_nameOfTextFile, "r");
string_text = file_fr.read();
print(string_text);
#Don't forget to close it
file_fr.close();
print("\nNew Section\n");
#New Function to download data
def downloadCSVData(string_CSVUrl):
buffer_Response = urllib.request.urlopen(string_CSVUrl);
object_csv = buffer_Response.read();
#Convert it to a String
string_csv = str(object_csv);
object_lines = string_csv.split("\\n");
#Print to a file
string_nameOfTextFile = "myText.txt";
#w for write, r for read
file_fw = open(string_nameOfTextFile, "w");
#Write the data, NOTE! this will erase and rewrite, not append
for aLine in object_lines:
file_fw.write(aLine + "\n");
#Don't forget to close it
file_fw.close();
#return string_csv;
#CSV sample (from Yahoo finance)
string_myCSVUrl = "http://real-chart.finance.yahoo.com/table.csv?s=GOOG&a=07&b=19&c=2004&d=03&e=26&f=2016&g=d&ignore=.csv";
downloadCSVData(string_myCSVUrl);
print("\nNew Section\n");
class neweggObject:
def __init__(self, string_imageUrl = "",
string_productName = "",
string_productUrl = "",
string_productRating = "0",
string_productRatingUrl = "",
string_productPrice = "$",
string_currentDate ="N/A"):
self.string_imageUrl = string_imageUrl;
self.string_productUrl = string_productUrl;
self.string_productName = string_productName;
self.string_productRating = string_productRating;
self.string_productRatingUrl = string_productRatingUrl;
self.string_productPrice = string_productPrice;
self.string_currentDate = string_currentDate;
#Web Crawler Section #
#Spider creation first
def tradeSpider(int_maxPages):
# Using the time import to set a date (import time up top)
int_currentResults = 1;
# List of neweggObjects that will be appended to
list_neweggObjects = [];
while int_currentResults <= (int_maxPages):
string_Url = "http://www.newegg.com/Product/ProductList.aspx?Submit=ENE&N=100007625&IsNodeId=1&bop=And&ActiveSearchResult=True&SrchInDesc=amd&PageSize=30&Page=" + str(int_currentResults);
object_returnedResults = requests.get(string_Url);
response_requestString = object_returnedResults.text;
soupObject = BeautifulSoup(response_requestString, "html.parser");
int_counter = 0;
#for object_productList in soupObject.findAll('span', {'class' : 'itemDescription'}):
# This loops through the stuff under the product list heading
for object_productList in soupObject.findAll('div', {'unbxdattr' : 'product'}):
# Empty params to be used in the object
string_imageUrl = None;
string_productUrl = None;
string_productName = None;
string_productRating = None;
string_productRatingUrl = None;
string_productPrice = None;
# This will cover the product Url and Name
for object_individualItem in object_productList.findAll('a', {'class' : 'itemImage'}):
object_href = object_individualItem.get("href");
object_title = object_individualItem.get("title");
string_productUrl = "Product Url: " + object_href;
string_productName = "Product Name: " + object_title;
#This will cover the produt image url
for object_imageDetails in object_individualItem.findAll('img'):
object_image = object_imageDetails.get("src");
string_imageUrl = "Image Url: " + object_image;
# This will cover the ratings
for object_individualItem in object_productList.findAll('a', {'class' : 'itemRating'}):
object_rating = object_individualItem.get("title");
string_productRating = "Rating: " + str(object_rating);
string_productRating = string_productRating.replace("Rating + " , "");
object_ratingUrl = object_individualItem.get("href");
string_productRatingUrl = "Reviews: " + str(object_ratingUrl);
# This will cover the Price information
for object_individualItem in object_productList.findAll('div', {'class' : 'itemAction'}):
for object_individualItem2 in object_individualItem.findAll('li', {'class' : "price-current"}):
# object_priceDollar = object_individualItem2.get("strong");
# object_priceCents = object_individualItem2.get("sup");
object_priceAmount = object_individualItem2.text;
string_s1 = str(object_priceAmount);
string_s1 = string_s1.replace("-", "");
string_s1 = string_s1.replace("_", "");
string_s1 = string_s1.replace("from", "");
string_s1 = string_s1.strip();
string_s1 = string_s1[:12]; # Need to fix why this has a trailing underscore. Regex?
string_s1 = "Current Price: " + string_s1;
string_productPrice = string_s1;
'''
Get the current date to set for logging purposes
Importing: 'from datetime import datetime'
The format matches this: '2013/10/05 00:20:30'
'''
date_currentDate = datetime.now();
string_currentDate = date_currentDate.strftime("%Y/%m/%d %H:%M:%S");
string_currentDate = "Data Retrieved: " + str(string_currentDate);
# Add the Strings to a new object
neweggObject_currentObject = neweggObject(string_imageUrl,
string_productName,
string_productUrl,
string_productRating,
string_productRatingUrl,
string_productPrice,
string_currentDate);
# Append it to the list of objects
list_neweggObjects.append(neweggObject_currentObject);
# Make sure to increment the results or be stuck in a perma-loop.
int_currentResults += 1;
return list_neweggObjects;
# Get a list of newegg Objects on pages 1 and 2
list_neweggObjects = tradeSpider(2);
# loop through and print the obejcts to test
for neweggObject in list_neweggObjects:
print(neweggObject.string_productName);
print(neweggObject.string_productUrl);
print(neweggObject.string_imageUrl);
print(neweggObject.string_productRating);
print(neweggObject.string_productRatingUrl);
print(neweggObject.string_productPrice);
print(neweggObject.string_currentDate);
print("\n");
print("\nNew Section\n");
"""
This is how to write a block, multi-line comment. Can either be one or two quotation marks btw
"""
# How to make a 'perma' / Dynamic web cralwer
def getSingleItemData(string_itemUrl):
# Do stuff
return 2;
# Call perma web crawler
getSingleItemData("rrr");
'''
Currently at:
https://www.youtube.com/watch?v=pLHejmLB16o&index=27&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_&spfreload=1
'''