Archive | GNU/Linux İpuçları

Learn How to Import, Create, Install, Upgrade, Alias and Reload Python Modules

Python is shipped with a lot of modules, we can say that there are python modules for almost everything you need. You need to work with robots, or some hardware in a spaceship, you will find a Python module for that. In this post, we will talk about Python modules and how to create, install, upgrade, reload and use them. The Python module allows you to organize your Python code and makes it easier to maintain later. Inside a module, you can define functions, classes, and variables. You can send your module to your friends and share it with others so they can use it.

Continue Reading →

Standard Library Modules

Python shipped with a large collection of modules. There are more than 200 modules for common programming tasks.

These modules called the standard library, you can find them in most installations on all platforms.

These modules can be used by importing them using the import statement as we’ve seen in the previous post Python programming basics.

How import statement works

Some newcomers might think that the import statement works like the include directive in C language or require in PHP, but that far from the truth because the other languages all it does is just grabbing the file content.

In Python, the situation is little different. There are three steps performed the first time a program imports a given file.

  1. Find the module’s file.
  2. Compile it to bytecode if needed.
  3. Build the objects from the bytecode.

You type the imported file without the extension and without its full directory path.

Then Python compiles it to bytecode. If the source file is modified, the bytecode file (.pyc file) regenerated automatically.

You deploy your Python programs as just bytecode files and avoid sending the source and Python will load the bytecode files.

The final step of the import operation is to execute the generated bytecode.

Python Search for a Module

As we said earlier, you just type the name of the module without extension or complete path and Python will take care of the rest.

So where Python does the search for the modules?

  1. The home directory of your program.
  2. PATH environment variable directories.
  3. Standard library directories.
  4. Any .pth files.
  5. The site-packages home of your third-party extensions.

The combination of these items becomes sys.path list.

Actually, sys.path is the path where Python searches for modules.

You can get your path like this:

import sys

print(sys.path)

You can add new custom paths to search for modules using sys.path.append or
sys.path.insert

When you type import mymodule, Python will load any of the followings:

  • Source code file named mymodule.py.
  • Bytecode file named mymodule.pyc.
  • Optimized bytecode file named mymodule.pyo.
  • A directory named mymodule, for package imports.
  • Compiled extension module like mymodule.so on Linux, or mymodule.dll on Windows.
  • Compiled module which is written in C.
  • ZIP file which is automatically extracted.
  • Jython files which are Python for Java.
  • .NET component, written in the IronPython version of Python

If you have two different files like mymodule.py and mymodule.dll, Python will load the first file found in the search path during the left-to- right search of sys.path. But what if python found both files in the same directory?

Any one of them can be loaded first, so stay away from falling in this situation.

Creating a Python Module

To create a Python module, nothing special is required, just open your editor and type your code and save your file with .py extension.

All defined functions of the module become its attributes.

For example, if you define a function like the following inside your module:

def myfunc:

print("welcome")

You can call the function myfunc from that imported module inside your code.

Keep in mind that when creating a module, the module name becomes a variable (without the extension) inside your Python program, so you can’t name a module if.py. Always avoid Python reserved keywords.

Installing Python Modules from Source

The module includes a special file named setup.py that handles the details.

You can install Python modules from the source like this:

$ python setup.py install

This command will install the module within the site-packages folder which could be like this C:\Python27\lib\site-packages

Or like this

C:\Users\LikeGeeks\AppData\Local\Programs\Python\Python36-32\lib\site-packages

Install Python Modules Using pip

You can use pip utility to install modules and packages from the PyPI repository.

Most developers prefer installing modules using this method. The best thing about pip is that it handles dependency checking.

You can install Python modules using pip like this:

$ pip install numpy

Also, you can upgrade your installed modules using pip like this:

$ pip install --upgrade numpy

This command will update all the dependencies of the modules.

Importing Modules

After writing your awesome modules or installing them, now you need to reuse it anywhere in your code, or maybe send it to your friends so they can use it, or share it with others on the web. How to import modules in your code?

There are two ways to achieve that, the first way is to use import statement, and the second way is to use from statement. Both of them will find, compile and run.

The major difference is that import statement fetches the whole module, while from statement fetches the specified attributes from the modules.

import mymodule

from mymodule import myfunc

The import Statement

Because import statement loads all attributes from the module so if you want to use any attribute, you have to use the module name like this:

import mymodule

mymodule.myfunc()

The from Statement

As we said before, the from statement loads only the specified attributes.

Using the from statement makes it less typing because we don’t have to specify the whole module name when calling it.

from mymodule import myfunc

myfunc()

Actually, behind the scenes the from statement is an extension of the import statement, the entire file is also loaded, but you will be provided with all attributes directly from your code.

You can write the from statement like this:

from mymodule import *

The asterisk here means import everything at the top level from the module.

The Danger of Using from Statement

Some Python developers recommend using the import statement over using the from statement.

But is this true? Does the from statement not safe?

Well, if you use the from to import variables, it will overwrite any variable that has the same name if exist without notice.

This problem, of course, doesn’t happen with the import statement. Since you type the module name to get its attributes, so no collision happens.

You should take care when using the from statement. Consider the following situation

#module1

def myfunc:

print("Hello from module 1")

#module2

def myfunc:

print("Hello from module 2")

Now, let’s import the above modules and try to use them

#MyCode

from module1 import myfunc

from module2 import myfunc

myfunc()

The output here will be from module 2 since the function will be overwritten.

Actually, this is not a common thing to encounter, but anyway, there is a solution for that.

Using Aliases

To avoid the name collision happened in the above example, you can use aliases like this:

#MyCode

from module1 import myfunc as myfunc1

from module2 import myfunc as myfunc2

myfunc1()

myfunc2()

It’s like a renaming the imports. Sometimes developers use aliases to shorten the module names they are importing when the module name is bigger.

After using aliases, do you think that using the from statements is dangerous? For me, it’s not.

Import Module Scope

You know that we can’t access the module attributes unless we import it.

Consider the following situation

#module1

M = 10

def myfunc():

global M

M = 20

#module2

M = 50

import module1

module1.myfunc()

print(M,module1.M)

When you run module2, the module1.myfunc() changes the M variable in module1 not the M in module2.

The result will be 50 20.

That means the global scope for module1.myfunc is the file that enclosing it, not the file it runs from.

The import statements don’t promote the visibility of the attributes, and the imported file can’t see the attributes in the importing file.

Reloading Modules

If you try to re-import the module after importing it on the top of your code, Python will fetch the already loaded module again.

To reload a module, you can use the reload function that can achieve that.

Why do we need to reload the imported module?

The answer is so simple. This allows some parts of your program to be modified without restarting your program. You can imagine any situation that needs dynamic customization.

Python can only reload modules written in Python only. So compiled extensions (like the discussed previously) can’t be reloaded.

You MUST import the module first before you reload it. You can reload modules like this:

import module1

module1.myfunc()

from importlib import reload

reload(module1)

module1.myfunc()

You can change the code of module1 in your text editor and it will be reloaded.

Reloading modules is a very powerful feature in Python if it is used carefully.

I hope you find the post useful and interesting. Keep coming back.

Thank you.

likegeeks.com

0

Cast or Convert an Array to Object Using PHP (Hydrator Pattern)

In this post, we will convert an array to object using PHP hydrator pattern. This method is so simple, it’s about transferring data from one place to another. We will define a class that will take an array and an object as inputs and search for all set() methods in the object and fills it with values from the array. First, we will determine the object class using get_class() function then we will use get_class_methods() to get the class methods.

 

Continue Reading →

class Converter
{
public static function toObject(array $array, $object)
{
$class = get_class($object);

$methods = get_class_methods($class);

foreach ($methods as $method) {

preg_match(' /^(set)(.*?)$/i', $method, $results);

$pre = $results[1] ?? '';

$k = $results[2] ?? '';

$k = strtolower(substr($k, 0, 1)) . substr($k, 1);

If ($pre == 'set' && !empty($array[$k])) {

$object->$method($array[$k]);
}
}
return $object;
}
}

Keep in mind that we use PHP 7 coalescing operator (??), in case you are not using PHP 7, you can use ternary operator instead.

We use substr then we concatenate because if we have lowerCamelCase member variables like $firstName.

To test this converter class, we need to create a class with properties and methods (getters & setters) and see how to convert an array to object using PHP in action.

Let’s assume that we have an employee class like this:

class Employee
{
protected $name;

protected $phone;

protected $email;

protected $address;

public function getName()
{
return $this->name;
}

public function getPhone()
{
return $this->phone;
}

public function getEmail()
{
return $this->email;
}

public function getAddress()
{
return $this->address;
}

public function setName($name)
{
$this->name = $name;
}

public function setPhone($phone)
{
$this->phone = $phone;
}

public function setEmail($email)
{
$this->email = $email;
}

public function setAddress($address)
{
$this->address = $address;
}
}

Convert Array To Object

Now let’s create an array that will hold the data that will be transferred to the class.

$arr['name'] = "Adam";

$arr['phone'] = "123456";

$arr['email'] = "[email protected]";

$arr['address'] = "U.S";

Great, let’s convert the array data to the class.

$obj = Converter::toObject($arr, new Employee());

var_dump($obj);

Look at the result:

Cool!!

You can convert an array to object using PHP hydrator pattern.

Convert Object to Associative Array

What about converting the object to an associative array, it’s the same concept, we are going to create a new function that does the opposite.

Our function will search for all get() functions the same way as the previous function like this:

public static function toArray($object)
{
$array = array();

$class = get_class($object);

$methods = get_class_methods($class);

foreach ($methods as $method) {

preg_match(' /^(get)(.*?)$/i', $method, $results);

$pre = $results[1] ?? '';

$k = $results[2] ?? '';

$k = strtolower(substr($k, 0, 1)) . substr($k, 1);

If ($pre == 'get') {

$array[$k] = $object->$method();
}
}
return $array;
}

Add this function to our converter class and call it with a passed object like this:

var_dump(Converter::toArray($obj));

Note that the passed $obj here is the generated object from the array to object conversion process.

The output shows the associative array as expected.

I hope you find the post useful. Keep coming back.

Thank you.

likegeeks.com

0

Process Large Files Using PHP

If you want to process large files using PHP, you may use some of the ordinary PHP functions like file_get_contents() or file() which has a limitation when working with very large files. These functions rely on the memory_limit setting in php.ini file, you may increase the value but these functions still are not suitable for very large files because these functions will put the entire file content into memory at one point. Any file that has a size larger than memory_limit setting will not be loaded into memory, so what if you have 20 GB file and you want to process it using PHP? Another limitation is the speed of producing output. Let’s assume that you will accumulate the output in an array then output it at once which gives a bad user experience. For this limitation, we can use the yield keyword to generate an immediate result.

Continue Reading →

SplFileObject Class

In this post, we will use the SplFileObject class which is a part of Standard PHP Library.

For our demonstration, I will create a class to process large files using PHP.

The class will take the file name as input to the constructor:

class BigFile
{
protected $file;

public function __construct($filename, $mode = "r")
{
if (!file_exists($filename)) {

throw new Exception("File not found");
}

$this->file = new SplFileObject($filename, $mode);
}

}

Now we will define a method for iterating through the file, this method will use fgets() function to read one line at a time.

You can create another method that uses fread() function.

Read Text Files

The fgets() is suitable for parsing text files that include line feeds while fread() is suitable for parsing binary files.

protected function iterateText()
{
$count = 0;

while (!$this->file->eof()) {

yield $this->file->fgets();

$count++;
}
return $count;
}

This function will be used to iterate through lines of text files.

Read Binary Files

Another function which will be used for parsing binary files:

protected function iterateBinary($bytes)
{
$count = 0;

while (!$this->file->eof()) {

yield $this->file->fread($bytes);

$count++;
}
}

Read in One Direction

Now we will define a method that will take the iteration type and return NoRewindIterator instance.

We use the NoRewindIterator to enforce reading in one direction.

public function iterate($type = "Text", $bytes = NULL)
{
if ($type == "Text") {

return new NoRewindIterator($this->iterateText());

} else {

return new NoRewindIterator($this->iterateBinary($bytes));
}

}

Now the entire class will look like this:

class BigFile
{
protected $file;

public function __construct($filename, $mode = "r")
{
if (!file_exists($filename)) {

throw new Exception("File not found");

}

$this->file = new SplFileObject($filename, $mode);
}

protected function iterateText()
{
$count = 0;

while (!$this->file->eof()) {

yield $this->file->fgets();

$count++;
}
return $count;
}

protected function iterateBinary($bytes)
{
$count = 0;

while (!$this->file->eof()) {

yield $this->file->fread($bytes);

$count++;
}
}

public function iterate($type = "Text", $bytes = NULL)
{
if ($type == "Text") {

return new NoRewindIterator($this->iterateText());

} else {

return new NoRewindIterator($this->iterateBinary($bytes));
}

}
}

Parse large Files

Let’s test our class:

$largefile = new BigFile("file.csv");

$iterator = $largefile->iterate("Text"); // Text or Binary based on your file type

foreach ($iterator as $line) {

echo $line;

}

This class should read any large file without limitations Great!!

You can use this class in your Laravel projects by autoloading your class and add it to composer.json file.

Now you can parse and process large files using PHP easily.

Keep coming back.

Thank you.

likegeeks.com

0

Install and Use Non-Composer Laravel Packages

If you want to use a package in Laravel, you simply add a single line in composer.json file and the job is done. This is because the package is available in packagist.org, what if the package that you want to use is a non-Composer Laravel package? Maybe available on a git repo or a private repo or so. In this post, we will see how to install and use non-Composer Laravel Package and custom libraries inside your Laravel projects.

Continue Reading →

Using a Git Package

For this purpose, I searched GitHub for a package that is not available on packagist.org and found one.

I’m going to use a package called UniversalForms from wesleytodd.

You can find it here https://github.com/wesleytodd/Universal-Forms-PHP

To use the package, open composer.json file and add it to the require section like this:

"wesleytodd/universal-forms" : "dev-master"

Then under the require section, add a new section called repositories like this:

"repositories": [
{
"type": "vcs",

"url": "https://github.com/wesleytodd/Universal-Forms-PHP"

}
]

Finally, run composer update

Now you can add the service provider for the package like any other composer package.

Open config/app.php and add the provider to the provider’s array.

'Wesleytodd\UniversalForms\Drivers\Laravel\UniversalFormsServiceProvider',

And you can use the package like any other package.

The best thing about this trick is that the repository will be treated like any Composer dependency and will put the package in vendor directory like magic.

Using Private Repositories

As you did with the GitHub repo, you can do the same with your private repos like this:

{

"require": {

"likegeeks/my-repo": "dev-master"

},

"repositories": [

{

"type": "vcs",

"url": "[email protected]:likegeeks/my-repo.git"

}

]

}

The only difference here is that you need to install the SSH keys for your git client.

This technique is supported by many git clients like:

  • Git
  • Subversion
  • Mercurial

Using Subversion

If you are using Subversion, it doesn’t have a native idea of branches and tags, so Composer will assume that the code is in $url/branches  and $url/tags.

If your repo has a different structure, you can change these value like this:

{

"repositories": [

{

"type": "vcs",

"url": "http://svn.website.com/projectA/",

"trunk-path": "MyTrunk",

"branches-path": "MyBranches",

"tags-path": "MyTags"

}

]

}

Autoload Custom Classes or Libraries

Now you can use your non-Composer Laravel packages inside your projects.

What if your package is not even on a repo, maybe a normal PHP library that contains classes or so how to use it inside your Laravel project?

Well, that is so simple. First, create a directory for storing your libraries let’s say app/libraries.

Then include the library file in composer.json file under classmap of autoload section like this:

{

"autoload": {

"classmap": [

"app/libraries/myLib.php"

]

}

}

This will include your file without problems, what if your library has a lot of files?

Great, you can include the directory name instead and Composer will load all of the classes automatically.

Now you can import and use any non-Composer Laravel packages.

I hope you find the post useful. Keep coming back.

Thank you.

likegeeks.com

0

Create and Use Dynamic Laravel Subdomain Routing

The subdomain is something we sometimes need. Many websites give their users a custom subdomain for their profiles or pages, so instead of accessing the user’s profile at http://website.com/users/50, the user can access his profile page at http://username.website.com which is much better. In this post, we will see how to make dynamic Laravel subdomain routing easily.

 

Continue Reading →

Configure DNS

To make this trick, you need to have access to the DNS server settings and apache web server settings.

First, you need to add an A record with an asterisk for the subdomain like this:

* IN A 192.168.1.5

You should replace the IP address with your IP address.

Configure Web server

Open apache web server configuration file httpd.conf and add a VirtualHost like this:

<VirtualHost *:80>
ServerName website.com
ServerAlias *.website.com
</VirtualHost>

Let’s assume that we have the users with the name field which will contain the user’s name.

Now we will create our route.

Route::get('/', function () {

$url = parse_url(URL::all());

$domain = explode('.', $url['host']);

$subdomain = $domain[0];

$name = DB::table('users')->where('name', $subdomain)->get();

dd($name);

// write the rest of your code.

});

First, we explode the URL and extract the host from it, then we get the subdomain part.

Then we search for a username in the users table that matches the extracted subdomain.

You can check if no user found, redirect to another page or give him an error message or whatever.

Now if you try to visit any user subdomain like http://likegeeks.website.com, you should see the user’s name without problems.

Keep in mind that the user that you are visiting his subdomain MUST be present in the database.

Any user added to the database will have his subdomain automatically without a headache.

If you don’t have access to your web server configuration like using shared hosting or so, you can’t achieve the same functionality using htaccess redirection.

Multiple Routes in Subdomain

In the above example, we use a single route to deal with the subdomain, but you can use many routes with a subdomain.

You can use routes groups to achieve this:

Route::group(array('domain' => '{subdomain}.website.com'), function () {

Route::get('/', function ($subdomain) {

$name = DB::table('users')->where('name', $subdomain)->get();

dd($name);

});
});

As you see, Laravel subdomain routing is very easy to implement.

I hope you find the post useful. Keep coming back.

Thank you.

likegeeks.com

0

Protect Forms using Honeypot Laravel Spam Protection Technique

Laravel framework has a powerful protection from CSRF, XSS, and SQL injection, but there is no built-in Laravel spam protection. If you need to protect your form from spam bots, you can use something like Akismet or reCaptcha which provide strong spam protection. OK, but your users maybe don’t like it because of the nag images that may appear from time to time or solving those puzzles to submit the form. For me, I find that the honeypot technique much better in many cases, where there is a hidden field in the form and when submitting that field, a validation rule will validate this field if it’s zero length or not and if the field value length is not zero, that means there is a bot trying to submit the form. Well, let’s get our hands dirty.

Continue Reading →

Laravel Spam Protection

In routes.php, the routes look like this:

Route::get('register', function()
{

return view('app');

});

Route::post('register','HomeController@store');

And in the views folder, your registration form or any kind of form will be like this:

{!! Form::open() !!}

{!! Form::label('email', 'Email') !!}

{!! Form::text('email', Input::old('email')) !!}

{!! Form::label('password', 'Password') !!}

{!! Form::password('password') !!}

{!! Form::text('honey_pot', '', array('style' =>'display:none')) !!}

{!! Form::submit('Register') !!}

{!! Form::close() !!}

In the controller file, you should write your validation rules like this:

public function store()
{

Validator::extend('honey_pot', function ($attribute, $value, $parameters) {

return $value == '';

});

$rules = array(

'email' => 'required|email',

'password' => 'required',

'honey_pot' => 'honey_pot'

);

$messages = array('honey_pot' => 'Nothing Here');

$validation = Validator::make(Input::all(), $rules, $messages);

if ($validation->fails()) {

return Redirect::to('register')->withErrors($validation)->withInput();

} else {

return "Awesome!!";

}

}

Don’t forget to add the use statements at the top of the controller file:

use Validator;

use Input;

In the above validation, we’ve created a new validator called honey_pot that checks for the field honey_pot and if the field is not empty, the rule will return false.

Now if any spambot tries to fill the form, it will fall in the honeypot field which should be zero length.

Creating a Middleware

You can apply this laravel spam protection technique for all Laravel forms, you can make a middleware to check for POST request if the honey_pot field is zero length or no.

First, create the middleware:

php artisan make:middleware HoneyBot

Then check for the honey_pot field in the middleware like this:

public function handle($request, Closure $next)
{
if($request->isMethod('POST') && count($request->honey_pot) != 0 ){

return redirect('register');
}
return $next($request);
}

And don’t forget to apply your middleware, you can apply it globally to all request by adding the middleware to the middleware array in app/HTTP/kernel.php  file.

\App\Http\Middleware\HoneyBot::class

Now you can remove the validation rules from the form and rely on the middleware Great!!

Another Solution

Some spam bots may be clever enough to detect those hidden fields and never touch it, so what is the solution for this?

Well, you can add another hidden field that carries the time of the form submission and when the user or the bot submits the form, you can check the time difference between the render time and the submissions time, if it’s less than three seconds or so, that means the submission comes from a spam bot.

There are ready to use packages for this purpose, but as you can see, the solution is in one simple line.

Again, I’m not saying that Google reCaptcha is not a good option, it’s actually an awesome option. If you have the chance to use it, don’t hesitate.

I hope you find the post useful. Keep coming back.

Thank you.

likegeeks.com

0

Prevent SQL Injection

Before we talk about how to prevent SQL injection, we have to know the impact of SQL Injection attack which is one of the most dangerous attacks on the web. The attacker can steal your data or even worse, the whole web server can be stolen from one SQL injection vulnerability. I wrote this post to show you how to prevent SQL injection. If you need to know more about SQL injection itself and its types and all other stuff, you can do a simple search on google if you want. The solution is to clean the request parameters coming from the user. Keep in mind that the solution that I’ll share with you is not like the most solutions on the web that go to every SQL statement and clean the request variables one by one. My solution is preventing SQL injection without messing with your CMS files.

Continue Reading →

Well, maybe someone who is using PHP would say that is easy, it is just using a function like mysql_real_escape_string or mysqli_real_escape_string.

But that works only on a single dimensional array, what about a multi-dimensional array?

Well, we need to iterate over array items recursively

So what we will do is preventing SQL injection against multidimensional array.

Solution

This code does the magic for both single and multidimensional arrays using PHP:

if (!function_exists("clean")) {

//Gets the current configuration setting of magic_quotes_gpc if on or off

if (get_magic_quotes_gpc()) {

function magicquotes_Stripslashes(&$value, $key) {

$value = stripslashes($value);

}

$gpc = array(&$_COOKIE, &$_REQUEST);

array_walk_recursive($gpc, 'magicquotes_Stripslashes');

}

function clean(&$value, $key) {

//here the clean process for every array item

// use mysqli_real_escape_string instead if you use php 7

$value = mysql_real_escape_string($value);

}

}

$req = array(&$_REQUEST);

array_walk_recursive($req, 'clean');

The PHP function used to walk through the request multidimensional array is array_walk_recursive.

Just put this code on the top of your site or header file right after connecting to the database, your file could be up.php or header.php or something similar.

Because if you use this code before the connection occurs, it will show an error because you are using mysql_real_escape_string function which needs a SQL connection.

If you are using PHP 7, you’ll notice that MySQL extension is removed, so in order to make the above code working, you need to replace the function mysql_real_escape_string with mysqli_real_escape_string.

One final thing I have to mention regarding your code, you should keep your SQL parameters on all of your pages between quotes like this:

mysql_query("select * from users where email='$email' order by id");

Notice how the variable $email is quoted.

Without quoting your input like the above statement, SQL injection prevention won’t work.

likegeeks.com

0