Author Archives: Dan Dumitrache

About Dan Dumitrache

Dan is a web developer based in Ireland and this is his WordPress blog.

How to open, read, and write text files in Python

Let’s say that you need to open, read, and write text files in Python. First you can use the open() built-in function to open the file. Once the file is opened you can use the two built-in methods read() and write() to read from the file and to write to the file.

>>> opened_file = open('file.txt')
>>> the_file_content = opened_file.read()
>>> the_file_content
'Hi there! I am a .txt file!'

The opened_file is a file descriptor object. In the above example, the read() method reads the entire content of the file. If you wish, you can read just a portion of the file, as is shown below:

Continue reading

05 – Selecting elements using child filters in jQuery

You can select elements in jQuery using CSS3 child pseudo-classes based on their position inside a parent element.

A few examples:

  • The ul li:last-child selects the last <li> of each <ul> element.
  • The div p:nth-child(2) retrieves all <p> elements inside a <div> that are the second child.
  • The div .main-div:nth-of-type(3) retrieves all the third elements having class main-div inside a <div>.

Note:

  • The :nth-child() counts all children elements regardless of their type and the index starts from 1 instead of 0.
  • The :eq() selects only the elements corresponding to the selector attached to the pseudo-class, regardless of how many siblings they have before them and the index starts from 0.

Continue reading

How to copy files using shutil module in Python

The shutil module contains many functions but two of them can be used to copy files from one location to another. To demonstrate this, I created two text files as shown in the image below:

copy files with shutil

You can copy the content of the file1.txt to the file2.txt. This way, both files will have the same content but they will still have their original names.

>>> import shutil
>>> new_file_path = shutil.copy('file1.txt', 'file2.txt')

Continue reading

How to create classes of iterators using itertools module in Python

The itertools module provides iterators that you can use in your projects. One of the commonly used method is accumulate.

>>> import itertools
>>> accumulator = itertools.accumulate(range(10))
>>> next(accumulator)
0
>>> next(accumulator)
1
>>> next(accumulator)
3
>>> next(accumulator)
6
>>> next(accumulator)
10
>>> next(accumulator)
15

Continue reading

04 – Selecting elements by filtering their position in jQuery

With jQuery you have the ability to select elements of a web page by their position. For example if you want to select the first <a> on the page you will write:

a:first

If you want to retrieve links starting from the third one on the page you will write the following code:

a:gt(1)

The gt means “Greater Than”. The argument 1 is the index from which to start. If you have any kind of programming background you will know that indexes usually start at 0. So, greater than 1 means the third element!

Continue reading

03 – Selecting elements using attributes in jQuery

Attribute selectors allow you to select elements based on their attributes. Let’s take a look at the ul part of our page.

<ul class="list-one">
		<li>
			<a href="http://dan.saigon.ro">My website contains:</a>
			<ul>
				<li><a href="http://dan.saigon.ro/about/">About Page</a></li>
				<li><a href="http://dan.saigon.ro/portfolio/">Portfolio Page</a></li>
				<li><a href="http://dan.saigon.ro/contact/">Contact Page</a></li>
				<li>And many other pages</li>
			</ul>
		</li>
		<li>I have other websites as well
			<ul>
				<li>About chemistry website</li>
				<li>About Math website</li>
			</ul>
		</li>
</ul>

Continue reading

02 – Retrieving elements by their hierarchy in jQuery

You may want to retrieve only the direct child of a certain element. To exemplify this we have to add a list (actually a list within another list) to our page. Here is what we have so far.

<!DOCTYPE html>
<html>
<head>
	<title>My site</title>
	<script src="jquery-3.2.1.min.js"></script>
	<style>
		.conclusion {
			font-size: 0.7em;
		}
		.red {
			color: red;
		}
	</style>
</head>
<body>

<div class="main-div">
	<h3>I am a header</h3>
	<p>I'm a paragraph</p>
	<p class="conclusion">This is a conclusion</p>
	<p class="conclusion red">This paragraph is a red conclusion</p>
	<img id='my-img' src='dan.jpg' alt="Dan pic">
	<ul class="list-one">
		<li>
			<a href="http://dan.saigon.ro">My website contains:</a>
			<ul>
				<li><a href="http://dan.saigon.ro/about/">About Page</a></li>
				<li><a href="http://dan.saigon.ro/portfolio/">Portfolio Page</a></li>
				<li><a href="http://dan.saigon.ro/contact/">Contact Page</a></li>
				<li>And many other pages</li>
			</ul>
		</li>
		<li>I have other websites as well
			<ul>
				<li>About chemistry website</li>
				<li>About Math website</li>
			</ul>
		</li>
	</ul>
</div>

<script>
	var $allElements = $('*');
</script>

</body>
</html>

Continue reading

01 – Basic selectors in jQuery

jQuery is all about manipulating HTML elements. In order to manipulate the HTML elements you have to select them. You can select them in different ways.

The All (Universal) selector

The All selector allows you to select all the elements of the page (all DOM elements of a page). It is represented by an asterisk, *.
Let’s consider a simple HTML page with different elements in it.

<!DOCTYPE html>
<html>
<head>
	<title>My site</title>
	<script src="jquery-3.2.1.min.js"></script>
</head>
<body>

<div class="main-div">
	<h3>I am a header</h3>
	<p>I'm a paragraph</p>
	<img id='my-img' src='dan.jpg' alt="Dan pic">
</div>

<script>
	var $allElements = $('*');
</script>

</body>
</html>

Continue reading

How to iterate over data that has no iterator in Python

Data structures in Python are almost entirely already iterable. However, sometimes you do need a generator for the cases when the data is not iterable.

A generator is just a function that yields back to the section where it was called from. This is a signal for Python that you want to create a generator therefore it will automatically save the state of the function at the point of the yield statement. At this point you can return to it when next() is called.

A classic example will be a generator that generates the sequence of squares.

>>> def squares(value = 5):
... 	while True:
... 		value = value + 1
... 		yield (value - 1) * (value - 1)
... 
>>> generator = squares()
>>> next(generator)
25
>>> next(generator)
36
>>> next(generator)
49

Continue reading