Posts

Showing posts from July, 2020

Can't find the code you're looking for? Just Suggest a Snippet and I'll add it :)

Can't execute 'conda activate' from bash script

Conda init you no longer need to add conda to path manually. Now we have the conda init command: step 1 conda init

SNAPPED!

How to comment out code in a bash or shell script

Comments bash script   single line:  #    multi line:  /*  */

SNAPPED!

Remove all duplicate files from folder and all sub directories linux ubuntu

Use the awesome fdupes package: fdupes --noprompt -d -r ~/folder

SNAPPED!

Embed HTML using prism

Use the language-markup class and replace the first < of each line with  &lt; Example: <pre class="language-markup" id="answer"><code>&lt;a href="your landing page url">     &lt;img src="your image url" /> &lt;/a></code></pre>

SNAPPED!

Make a link clickable HTML

TEXT <a href="your landing page url"> <img src="your image url" /> </a>

SNAPPED!

Append to a json file python

How to append to a json file python - the quick way import json your_dictionary = {'a_new_key': 'a_new_value'} with open('a_json.json') as a_file: data = json.load(a_file) data.update(your_dictionary) with open('a_json.json', 'w') as f: json.dump(data, f)

SNAPPED!

prettify or clean a JSON with python

prettify or clean a JSON with python import json with open('cover_letter_text_parsed_v3.json', 'r') as a_file: parsed = json.load(a_file) with open('cover_letter_text_parsed_v3_clean.json', 'w') as a_file: json.dump(a_file, handle, indent=4, sort_keys=True)

SNAPPED!

Read a JSON file python

Image
Read Json using json.load with open('a_file.json') as file: data = json.load(file) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

print exception python except statement

Image
except Exception as e: print(str(e)) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

get all hrefs on page selenium python

Image
elems = driver.find_elements_by_xpath("//a[@href]") for elem in elems: print(elem.get_attribute("href")) Or see shorter snippets below: .find_elements_by_xpath("//a[@href]") driver.find_elements_by_xpath("//a[@href]") SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Convert HTML to Text Python

Image
html2text  is a great library for this and easy to use! import html2text plain_text = html2text.html2text("<p>I am HTML</p>") SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Generate a random Number between 0 and 1 (or 0 and less than 1) python

Image
import random random.uniform(0, 1) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Tool for formatting inputs for selenium css selectors for multi class names (with spaces) - selenium python

Image
When you use selenium to select an element with multiple classes using driver.find_element_by_css_selector(), you need to replace spaces with dot notation. For example, To select The following element:  <i align="center" class="far fa-copy croc-copy-icon">  you 'll need to use the following selector with dot notation formatting: driver.find_element_by_css_selector(".far.fa-copy.croc-copy-icon”) Having to replace spaces with full stops over and over again can get repetitive and time-consuming especially for lengthy classes. Here is a quick script I use to replace all spaces with dots, and add a dot at the beginning of the string. I found this can save quite a bit of time import pyperclip text = "a really long multi class element t-14 t-black--

SNAPPED!

Get all child elements Selenium python inner HTML (innerHTML)

Image
You can use the get_attribute() method .get_attribute('innerHTML') SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Infinite scroll selenium python

Image
One way to handle infinite scroll is by using the height of the viewport. Here we continue to scroll until the viewport height before and after does not change, hence, all elements on the page have been loaded import time def scroll_infinite(driver, wait_time, number_of_times_to_scroll): # Current scroll height current_height = driver.execute_script("return document.body.scrollHeight") while True and number_of_times_to_scroll > 0: # Scroll to bottom of page driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait for new content to load time.sleep(wait_time) # Calculate new scroll height n_height = driver.execute_script("return document.body.scrollHeight") if n_height == current_height: break number_of_times_to_scroll -= 1 current_height

SNAPPED!

get submit button or sign in button Selenum python xpath

Image
More often then not, sign buttons have the "submit" attribute. sign_in_button = driver.find_element_by_xpath('//*[@type="submit"]') SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Get current URL selenium python

Image
Use the current_url method: driver.current_url SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Press ENTER selenium python

Image
To press enter on an element such as a text field or text area, import Keys and use Keys.ENTER .send_keys(Keys.ENTER) Example: from selenium.webdriver.common.keys import Keys element.send_keys(Keys.ENTER) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Type into field or text box selenium python

Image
Use the  .send_keys() method: .send_keys("Hello!") Example: driver.find_element_by_class_name("input-field").send_keys("Hello!") SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Click an element selenium python

Image
To click an element you can use the .click() method element.click() SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Clear text field, form field or text-area selenium python

Image
Use the .clear() method See example below search_bar = driver.find_element_by_tag("search") search_bar.clear() SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Change Maximum or max number of posts shown on main page Blogger

Image
How to change max number of posts shown on the Home page of your blogger blog In case your looking for this setting - it's under "settings" > posts

SNAPPED!

Setup Mailhog mail server on Ubuntu

Image
How to setup Mailhog mail server on Ubuntu install Go (since mailhog is written in Go) sudo apt-get install golang-go mkdir gocode echo "export GOPATH=$HOME/gocode" >> ~/.profile source ~/.profile install mailhog go get github.com/mailhog/MailHog go get github.com/mailhog/mhsendmail make binaries global - replace USERNAME with your OS username sudo cp /home/USERNAME/gocode/bin/MailHog /usr/local/bin/mailhog sudo cp /home/USERNAME/gocode/bin/mhsendmail /usr/local/bin/mhsendmail install PHP sudo apt install php7.2-cli edit php.ini

SNAPPED!

Install Postgres specific version Ubuntu

In this guide we'll install Postgresql 9.6.11. If you need to install another version, just replace "9.6.11" with your desired version. First remove any existing version: sudo apt-get --purge remove postgresql dpkg -l | grep postgres sudo rm -rf postgresql Install postgres - specific version sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add - sudo apt-get update sudo apt-get upgrade sudo apt-get install postgresql-9.6.11 Change default password. By default, the postgres user password will default to the ubuntu user password.

SNAPPED!

python selenium cant find elements in iframe

To find elements within an iframe using selenium, you’ll need to use the driver’s switch_to.frame() method: i_frame = driver.find_element_by_id("iframe_id") driver.switch_to.frame(i_frame) Now you can locate elements as normal Why does this happen? Selenium initally looks for the first HTML tag available in the page. However, iframes have their very own second HTML tag , thus its necessary to switch back and forth when finding elements.   SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

tqdm module not callable

This is caused by trying to import tqdm using " import tqdm ". Instead, import as a comparable, as follows: from tqdm import tqdm for i in tqdm(range(10)): print(i) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Progress bar python

Tqdm is a great and easy progress bar for python. Here are some examples:  Iterating through lists : from tqdm import tqdm a_list = [1,2,3,4,5] for i in tqdm(a_list): print(i) You can use it on generators : from tqdm import tqdm for i in tqdm(range(10)): print(i) And even iterating through files : import csv from tqdm import tqdm with open('names.csv', newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) for row in tqdm(reader): print( row['first_name'], row['last_name'] ) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNA

SNAPPED!

ijson example : read large json file too big to fit into memory

Working with large JSON files and need to read them without loading into memory? ijson to the rescue! ijson is essentially a generator that can read JSON files :D from pprint import pprint import ijson as ijson with open("path/to/file.json", 'rb') as json_file: for item in ijson.items(json_file, "item"): pprint(item['one'], width=1000) pprint(item['two'], width=1000) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Read tsv python no column header

import csv with open(r"file.tsv", encoding="utf-8") as tsv_file: read_tsv = csv.reader(tsv_file, delimiter="\t") for row in read_tsv: print( row[0], row[1], ) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! COPIED

SNAPPED!

Customize scrolling and scroll bar HTML CSS

Tired of clunky, windows 95 scroll bars for your website? why not spice them up with a little css! For example, here is the scroll bar styling for this very site codingcroc.com :) /* width */ ::-webkit-scrollbar { width: 20px; } /* Scroll bar track */ ::-webkit-scrollbar-track { box-shadow: inset 0 0 5px grey; border-radius: 10px; } /* Scroll bar handle */ ::-webkit-scrollbar-thumb { background: #919191; border-radius: 10px; } /* Scroll bar handle on hover */ ::-webkit-scrollbar-thumb:hover { background: chartreuse; } SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Python Class Basic Example

class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("Padme Amidala", 26) print(p1.name) print(p1.age) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! COPIED

SNAPPED!

python selenium example (boilerplate) getting started. [search shortcut: sel basic ]

Python Selenium enables you to control a web browser using Python. Here's some boilerplate code I use to get started! from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import NoSuchElementException from selenium.webdriver import ActionChains # example of adding options option = Options() options.add("--headless") driver = webdriver.Chrome(options=option, executable_path="path/to/chromedriver.exe") url = "https://www.codingcroc.com" driver.get(url) driver.find_element_by_id('element') driver.find_element_by_name('element') driver.find_element_by_xpath('element') driver.find_element_by_link_text('element') driver.find_element_by_partial_link_text('element') driver.find_element_by_tag_name('element')

SNAPPED!

python f strings

f-strings allow you to place values and variables inside a string,  which is cleaner than using "{}".format(var) . f"I'm an f-string with a variable: {variable} :D" Full example: animal = "Goat" street_number = 10 f"There's a {animal} living in unit {street_number} down the road?" since f-strings are evaluated at runtime, you can also do calculations or call functions within them: city = "Melbourne" f"My favourite city is {city.title()}" For multi-line, be sure to add "f" infront of each line: city = &

SNAPPED!

How to get the index for items in a 'for' loop

a_list = ["icecream","lemon cake","chocolate cake"] for index, value in enumerate(a_list): print(index, index) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

read csv python dictionary dict.read

Image
import csv with open('names.csv', newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print( row['first_name'], row['last_name'] ) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

write to csv python dictionary dict.write

import csv with open('names.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow( { 'first_name': 'Padme', 'last_name': 'Amidala' } ) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Get all words or characters between two characters python

text_in_between = string.split(first_word)[1].split(second_word)[0] Full example: string = "Once upon a time" first_word = "Once" second_word = "time" text_in_between = string.split(first_word)[1].split(second_word)[0] print(text_in_between) SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

Use aria-label to find element with python selenium

driver.find_element_by_css_selector("[aria-label=LABEL_TITLE]") SEND TO YOURSELF, SAVE FOR LATER, OR SHARE WITH YOUR TEAM! SNAPPED!

SNAPPED!

blogger CSS disappeared or can't find

Image
Sometimes in blogger.com, when you try to edit CSS ( Theme > Customize > Advanced > Add CSS ), you find it missing! This is usually a result of changes made to your Theme's HTML, such as adding a CSS file or a script. However, you'll notice that nothing has changed on your site - so clearly your custom CSS is still there. Whilst I'm not exactly sure what the cause is, my guess it that this is not a bug, but rather a symptom of design. Without being able to see code base of blogger, my guess is when you first select your theme, there an initialisation script that runs which instantiates a reference point for "add CSS" component. However, when you make custom changes to the HTML, this reference point is lost either due to it not being updated, or possibly - it is overwritten to be empty.  How to fix When you 'add CSS', blogger just appends it onto the theme's HTML page within the  <b:skin> tag. So the fix is to grab it back! (a bit hacky i

SNAPPED!

Popular posts from this blog

Append to a json file python

Setup Mailhog mail server on Ubuntu