Python code – Devstyler.io https://devstyler.io News for developers from tech to lifestyle Fri, 22 Oct 2021 12:22:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Here is the Complete Guide to File Handling in Python https://devstyler.io/blog/2021/10/22/a-complete-guide-to-file-handling-in-python/ Fri, 22 Oct 2021 09:38:29 +0000 https://devstyler.io/?p=73687 ...]]> The Python programming language allows users to read files and write them into files. The Python programming language also supports file handling and offers a variety of tools to manipulate files, including the ability to read and write files.

A majority of the values of data stored in programming languages are in some volatile variables. These variables will only store data during runtime and be deleted once the program’s execution is complete. It is, therefore, better to store these data in files permanently.

How does Python Handle Files?

As a result of the volatile nature of variables, large software applications can’t store their data in them.

So, when you have to deal with situations like these, the role of files becomes evident. As files are nonvolatile, the data will be built up in a secondary device like a Hard Disk. Various other languages use the concept of file handling, but their implementations are either complicated or lengthy; with Python, this concept is simple and short.

The Python language treats binary and text files differently, and this is important. There is a sequence of characters in every line of code, and they form a text file.

Python File Handling Operations

Files can be either text or binary in nature. A text file contains data that humans can read. In contrast, binary files contain a series of 1s and 0s. Unlike binary files, text files are composed of text. The text file format is described here.

Python Create and Open a File

File names and file opening modes are passed to open() as arguments. In addition to the filename, the file opening mode can be used to specify how to open the file when using the open() function. Тhe syntax in the following example specifies that a minimum of one argument must be given.

A file object is returned by the open method, which allows access to in-built functions such as write, read, and others.

Syntax:

file_object = open(file_name, mode)

In this case, file_name is the file’s name or the file location you want to open, and file_name should also have the file extension. The filename is test.txt, and the extension is .txt.

Python Read From File

The file must be opened in reading mode for Python to read it.

Python provides three ways to read files.

  • read([n])
  • readline([n])
  • readlines()

It is the number of bytes to be read here.

Behind the scenes, Python does all the heavy lifting when reading files. The script can be run by entering ‘python’ followed by the name of the file in the Command Prompt – or Terminal – and navigating to the file.

Python Write to File

To write data into a file, you must open the file in write mode.

Python’s open() method is used for writing to files. To write to a file, you must pass a filename and a special character to Python.

The data you write into the file overwrites the previous data, so you need to take care when you are writing data.

Python opens files in write mode when ‘w’ is passed as an argument to open(). The new data overwrites any content already existing in the file.

The Python interpreter will create the file if it does not exist. The program will create a file named “sample.txt” when it runs.

Using the Command Prompt, run the following program:

Python Close File

A file must first be opened to be closed. The close() method in Python is used to close opened files.

It’s important to close a Python file after using it. Closing a file prevents a program from accessing its contents.

The method close() closes a file.

Opened files should be closed immediately, especially with the write method. You cannot save data into a file if you do not call the close function after the write method.

An understanding of the open() method is necessary for reading and writing files in Python. In Python, you can read, write, and create files by using this method’s versatility. Binary or text files can be Python files. With Python, data can be processed in virtually endless ways once it is loaded. It is common for programmers to generate many files automatically through programs.

To learn more about computer programming and security, we recommend the following resource guides that were recently discussed on the site. Start with our VPN guide, and then move on to our server optimization report.

]]>
New Data Science Platform Speeds up Python queries https://devstyler.io/blog/2021/07/07/new-data-science-platform-speeds-up-python-queries/ Wed, 07 Jul 2021 05:46:06 +0000 https://devstyler.io/?p=57925 ...]]> Researchers from Brown University and MIT have developed a new data science framework that allows users to process data with the programming language Python—without paying the ‘performance tax’ normally associated with a user-friendly language.

The new framework, called Tuplex, is able to process data queries written in Python up to 90 times faster than industry-standard data systems like Apache Spark or Dask. The research team unveiled the system in research presented at SIGMOD 2021, a premier data processing conference, and have made the software freely available to all. Malte Schwarzkopf, an assistant professor of computer science at Brown and one of the developers of Tuplex commented:

“Python is the primary programming language used by people doing data science. That makes a lot of sense. Python is widely taught in universities, and it’s an easy language to get started with. But when it comes to data science, there’s a huge performance tax associated with Python because platforms can’t process Python efficiently on the back end.”

Platforms like Spark perform data analytics by distributing tasks across multiple processor cores or machines in a data centre. That parallel processing allows users to deal with giant data sets that would choke a single computer to death. Users interact with these platforms by inputting their own queries, which contain custom logic written as “user-defined functions” or UDFs. UDFs specify custom logic, like extracting the number of bedrooms from the text of a real estate listing for a query that searches all of the real estate listings in the U.S. and selects all the ones with three bedrooms.

Because of its simplicity, Python is the language of choice for creating UDFs in the data science community. In fact, the Tuplex team cites a recent poll showing that 66% of data platform users utilize Python as their primary language. The problem is that analytics platforms have trouble dealing with those bits of Python code efficiently.

Data platforms are written in high-level computer languages that are compiled before running. Compilers are programs that take computer language and turn it into machine code—sets of instructions that a computer processor can quickly execute. Python, however, is not compiled beforehand. Instead, computers interpret Python code line by line while the program runs, which can mean far slower performance. Schwarzkopf said:

“These frameworks have to break out of their efficient execution of compiled code and jump into a Python interpreter to execute Python UDFs. That process can be a factor of 100 less efficient than executing compiled code.”

If Python code could be compiled, it would speed things up greatly. But researchers have tried for years to develop a general-purpose Python compiler, Schwarzkopf says, with little success. So instead of trying to make a general Python compiler, the researchers designed Tuplex to compile a highly specialized program for the specific query and common-case input data. Uncommon input data, which account for only a small percentage of instances, are separated out and referred to an interpreter. Leonhard Spiegelberg, the co-author of the research describing Tuplex, also noted:

“We refer to this process as dual-case processing, as it splits that data into two cases. This allows us to simplify the compilation problem as we only need to care about a single set of data types and common-case assumptions. This way, you get the best of two worlds: high productivity and fast execution speed.”

And the runtime benefit can be substantial. Schwarzkopf also said:

“We show in our research that a wait time of 10 minutes for output can be reduced to a second. So it really is a substantial improvement in performance.”

In addition to speeding things up, Tuplex also has an innovative way of dealing with anomalous data, the researchers say. Large datasets are often messy, full of corrupted records or data fields that don’t follow convention. In real estate data, for example, the number of bedrooms could either be a numeral or a spelt-out number. Inconsistencies like that can be enough to crash some data platforms. But Tuplex extracts those anomalies and sets them aside to avoid a crash. Once the program has run, the user then has the option of repairing those anomalies.

]]>
5 Python Books to Transfer Your Code to The Next Level https://devstyler.io/blog/2021/06/09/5-python-books-to-transfer-your-code-to-the-next-level/ Wed, 09 Jun 2021 17:11:14 +0000 https://devstyler.io/?p=54245 ...]]> Whether you are in data science, web development, or any technical field that requires code design and writing, you must have used Python. Python is one of the most popular and used programming languages at the moment. Python is so popular because of its simple syntax, how easy it’s to learn for new programmers or programmers shifting from other programming languages, and how versatile it is.

Although Python is easy to learn and use, writing good Python code is not as simple. It is a skill that requires writing a lot of code and being able to make it look pretty. Writing a code is what differentiates an amateur programmer from an experienced one.

For your code to be beautiful, it needs to be easy to read and comprehend, lucid, and well balanced. It is a code that’s architecture, structure, and overall shape can tell you its intent without digging deeper into its application. Beautiful code often is divided into solid small pieces, each with its own purpose.

Most programmers can write code and solve problems, but you will stand out within the field if you can write beautiful solid code. In today’s article, I am addressing all levels of programmers; whether you’re a beginner, intermediate or advanced programmer, you can probably improve the quality of your code. This article will go through 5 Python books that focus on improving your code quality rather than teaching you how to code.

№1: Supercharged Python: Take Your Code to the Next Level

This book was written by John Bennett, Brian Overland, and Brian Overland and was published in 2019. Like all other books in this list, this book targets those who already know the fundamentals of programming in general and know how to write basic code in Python.

This book will teach you how to harness the full power of Python and — as the title suggests — supercharge your applications. The book covers advanced list and string techniques, how to manage and handle binary files, how to master commonly used packages such as Numpy, and how to write solid concrete classes.

The book also addresses profilers, the magic of regular expression, and teaches you how to take advantage of 22 coding shortcuts and performance-enhancing tips and tricks to optimize your code execution. In a nutshell, the Supercharged Python teaches you how to write powerful, sophisticated Python applications faster and more efficiently.

№2: Python Tricks: A Buffet of Awesome Python Features

Python Tricks by the legend Dan Badar was published in 2017. If you’re looking for a good, concise book that teaches you how to write more “Pythonic” code, look no further, this book is for you.

Python Tricks illustrates valuable yet often overlooked Python features and best practices that will help you better understand Python and hence be able to write better codes using it. This book contains 43 subsections, each more valuable than the other and each containing a straightforward explanation with simple examples of how to use the proposed concepts.

This book covers many advanced topics such as commonly used patterns for cleaner Python code, how to effectively used functions, loops, and dictionaries, the best practices of object-oriented programming and classes, and some Pythonic productivity techniques. Moreover, this book comes with 12 bonus videos explaining the different concepts proposed in the book using short 11 minutes videos that you can listen to or watch any time and anywhere.

№3: Effective Python: 90 Ways to Write Better Python

Next up is a gem of a book written by Brett Slatkin in 2015, which is Effective Python: 90 ways to write better Python. This book covers 90 different. Concise and well-explained items discussing different aspects and concepts you can jump between in any order you find comfortable.

When this book was originally published, it only contained 59 ways to write better Python code, but the second edition expanded to include 30+ more new and useful concepts. Concepts like Pythonic thinking, functions, classes and inheritance, masterclasses, and concurrency and parallelism.

The book also covers — in-depth — how to package your code properly, write efficient documentation and manage dependency and virtual environment. Moreover, maintain your package by debugging it, testing it, and handling memory management for optimal execution. So, if you are done learning Python basics and want to go deeper into the language, this book is for you.

№4: Practices of the Python Pro

Next up is a relatively new book, Practices of the Python Pro, written by Dane Hillard and published in early 2020. This book targets professional programmers who want to write clean, well-organized, and maintainable application code using Python.

This book teaches you how to design, develop and write high-quality, professional code. Code that’s easy to understand, maintain and extend. The book focuses on giving you a solid foundation of how, why, and when to modularize your code, improve its quality, and reduce its overall complicity with solid and clear examples. Learning the techniques covered in this book, your code will be easy to read and reuse by others.

Practices of the Python Pro consist of mainly 4 sections. An introductory and a conclusion section and two sections cover all you need to produce beautiful code. Sections 2 and 3 take you through the process and mindset of designing large-scale software systems and how to make them solid, maintainable, and easy to browse.

№5: Intuitive Python: Productive Development for Projects that Last

Let’s conclude our list with the newest book of the batch, Intuitive Python, written by David Muller and just got published this year. Unlike all other books on this list, Intuitive Python goes beyond writing good, beautiful code in Python. This book provides you with the tools and packages you need to maintain your code.

Reading this book, you will learn how to run static and dynamic analysis tools on your code to detect and eliminate bugs in your code before you package and release it. You will also learn how to download and run third-party Packages without compromising the safety of your code. Furthermore, you will learn the most effective way to use the Python default debugger and use its interactive console.

]]>
How to Create a Discord Bot for Free with JavaScript https://devstyler.io/blog/2021/06/03/how-to-create-a-discord-bot-for-free-with-javascript/ Thu, 03 Jun 2021 11:10:37 +0000 https://devstyler.io/?p=53332 ...]]> How to Create a Discord Bot Account

In order to work with the Node.js library and the Discord API, we must first create a Discord Bot account.

Here are the step to creating a Discord Bot account.

  1. Make sure you’re logged on to the Discord website.
  2.  Navigate to the application page.
  3.  Click on the “New Application” button.
  4.   Give the application a name and click “Create”.
  5.  Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking “Yes, do it!”

Keep the default settings for Public Bot (checked) and Require OAuth2 Code Grant (unchecked).

Your bot has been created. The next step is to copy the token.

How to Invite Your Bot to Join a Server

Now you have to get your Bot User into a server. To do this, you should create an invite URL for it.

Go to the “OAuth2” tab. Then select “bot” under the “scopes” section. Now choose the permissions you want for the bot. Our bot is going to mainly use text messages so you don’t need a lot of the permissions. After selecting the appropriate permissions, click the ‘copy’ button above the permissions. That will copy a URL which can be used to add the bot to a server. Paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.

To add the bot, your account needs “Manage Server” permissions. Now that you’ve created the bot user, start writing the Python code for the bot.

How to Create a Repl and Install discord.js

Start by going to Repl.it. Create a new Repl and choose “Node.js” as the language. This means the programming language will be JavaScript.

To use the discord.js library, just add const Discord = require(“discord.js”); at the top of main.js. Repl.it will automatically install this dependency when you press the “run” button.

How to Run the Bot

Now click run button on the top to run your bot in repl.it and go to your Discord room and type “ping”. Your bot should return “pong“.

How to Enable User-submitted Messages

The bot is completely functional, but now let’s make it possible to update the bot right from Discord.
Use Repl.it’s built-in database to store user-submitted messages. This database is a key-value store that’s built into every repl.

How to Set Up the Bot to Run Continuously

If you run your bot in repl.it and then close the tab it is running in, your bot will stop running. But there is a way you can keep your bot running continuously, even after you close your web bowser.

The simplest way is to sign up for paid plan in Repl.it. Their cheapest paid plan is called the Hacker Plan and it includes five always-on Repls.

Once you have signed up for that plan, open your Repl and click the name at the top. Then select the “Always On” option.

How to Create a Web Server in repl.it

Creating a web server is simpler than you may think. To do it, create a new file in your project called server.js. The server will run on a separate thread from the bot.

]]>
Faster warmup, smaller downloads, JDK 16 — GraalVM 21.1 is here! https://devstyler.io/blog/2021/05/27/faster-warmup-smaller-downloads-jdk-16-graalvm-21-1-is-here/ Thu, 27 May 2021 12:25:00 +0000 https://devstyler.io/?p=52442 ...]]> Platform Updates

Oracle GraalVM Enterprise Edition 21.1 is based on Oracle JDK version 1.8.0_291 and Oracle JDK version 11.0.11. GraalVM Community Edition in 21.1 is based on OpenJDK version 1.8.0_292 and OpenJDK version 11.0.11.

GraalVM 21.1 introduces new experimental binaries based on JDK 16 for both Enterprise and Community editions based on JDK 16.0.1.

The experimental status means that all components in the JDK 16 based binaries are considered experimental regardless what their status is in other distribution versions.

The JIT mode for Java applications is perhaps the most tested capability for these builds, so if you are interested in running your Java applications with the GraalVM compiler or are currently using the JVMCI compiler in any other JDK 16 OpenJDK builds, consider trying out the GraalVM binaries. These include the latest OpenJDK changes and the latest GraalVM compiler changes which is the best of both worlds setup.

Java 16 is the current release of Java and we are looking forward to providing support for the upcoming Java 17 LTS release. Note that due to the decommissioning of the aging macOS 10.7 build infrastructure, GraalVM Community Edition releases based on JDK 8 are no longer being built. Node.js included in GraalVM 21.1 has been updated to 14.16.1, which is recommended for most users.

There’s one more significant change regarding Node in GraalVM. As of GraalVM 21.1, Node.js support is no longer included in the base GraalVM download. It’s now a separate component that you can install with the gu install nodejs command. JavaScript support continues to be a part of the base download, it’s just the Node.js support that’s installable separately. This change is for speed and clarity — it aims to reduce the size of the base GraalVM download, and to reduce confusion among some users who want to use GraalVM primarily as their main JDK.

Compiler Updates

Updates to the compiler are especially exciting because they improve GraalVM across the board since the compiler underpins the performance of all the various languages supported on GraalVM!

In 21.1 there are two particularly interesting compiler improvements:

One new optimization eliminates unneeded memory barriers on sequential volatile writes on x86. Numerous volatile writes sometimes occur in a sequence after the code has been inlined. The GraalVM compiler now omits the memory barrier for all but the last write in the sequence, nicely speeding up methods like ConcurrentHashMap.transfer().

Loops Updates

This release includes support in the GraalVM Enterprise compiler for vectorizing loops that have the hashCode-like pattern. Hashcode is often computed with an idiom like: hash = c * hash + array[i] , which the compiler can now recognize and vectorize.

Another improvement in this regard is a novel loop inversion optimization. This adds compiler support to GraalVM Enterprise to generate inverted loops from regular ones. Inverted loops have superior characteristics for instruction-level parallelism and optimization capabilities compared to regular, head counted loops.

Native Image

GraalVM 21.1 adds support for multiple locales in Native Image. Now you can specify at build time which locales should be included in the generated executable. For example, to switch the default locale to German and also include French and English, use -H:DefaultLocale=de -H:IncludeLocales=fr,en. All locales can be included via –H:+IncludeAllLocales. ResourceBundles are included by default for all selected locales, but this can be changed by providing a locale-specific substring when requesting the bundle.

JavaScript

The version of Node.js GraalVM supports has been updated to 14.16.1. In this release, the node and npm binaries are not included in the base download, but are instead available to be installed separately.

JavaScript in GraalVM 21.1 includes Truffle’s support for iterators, iterables, and byte buffers, which allows JavaScript iterators to be used via the Value API hasIterator(), getIterator(), hasIteratorNextElement(), getIteratorNextElement(); iterable objects from other languages to be iterated in GraalVM’s JavaScript runtime, e.g., via for-of loops and vice versa; for the host ByteBuffers and buffers from other languages to be used with JavaScript typed arrays, e.g., new Uint8Array(foreignBuffer), and DataView without copying; and to access ArrayBuffers via the Value API: readBuffer*, writeBuffer*.

Python

One of the more significant improvements in GraalVM 21.1 for Python is the enhanced support for Java subclassing and new interop APIs for better Jython migration path. Iteration over Python types from Java, catching and re-throwing Java exceptions in Python code, as well as implementing Java abstract classes and interfaces from Python are often-requested Jython features that GraalVM now provides, making the migration easier.

Tools

Tooling is a very important part of the GraalVM Ecosystem. In every release we’re trying to improve the developer experience by improving the tools that are part of the GraalVM distribution, including the debugger and profilers, etc. GraalVM 21.1 is no exception.

For example, VisualVM now also works on JDK 16 and has support for running on Apple M1 chips. The new version of the VS Code GraalVM Extensions, which includes a collection of features to make working with GraalVM, Java, and Micronaut applications easier:

Added unit test results visualization

  • Improved Micronaut support: YAML support and content assistance for configuration files, and more Java code editing features
  • Added new supported refactorings for Java code
  • Added support for Docker build commands for Micronaut projects
]]>