17:59, 27th July 2021
Linux Add User To Group Using Command-Line
On Linux, users can belong to two types of groups: a primary group, which is applied at login and governs ownership of files and processes, and secondary groups, which allow access to shared resources and can be used to fine-tune system security. The useradd command is used to create new users and assign them to groups during account creation, while the usermod command is used to modify existing user accounts, including group membership.
To add a user to a supplementary group without removing them from their current groups, the usermod command should be used with both the -a and -G flags, for example usermod -a -G groupname username. New groups can be created using the groupadd command and removed using the groupdel command.
User and group information is stored across several system files, including /etc/passwd, /etc/shadow and /etc/group, though these should not be edited by hand. Group membership for a given user can be verified using either the id or groups command, and all groups on the system can be viewed by reading /etc/group or using the getent command.
14:12, 26th July 2021
These OpenCV-Python tutorials offer a structured guide to using the OpenCV library for computer vision tasks, covering essential topics such as setting up the environment, handling graphical user interface elements, performing core image manipulations and applying advanced techniques in image processing, feature detection, video analysis, camera calibration, machine learning, computational photography and object detection. Additional sections explore the development of Python bindings and provide practical examples to support learning and implementation of vision-related algorithms.
19:07, 24th July 2021
How to Exclude Files and Directories with Rsync
The rsync command-line utility offers flexible options for controlling which files and directories are included or excluded during a synchronisation operation. Using the --exclude flag, users can skip specific files or directories by passing their relative paths, while multiple exclusions can be handled either through repeated --exclude flags, shell brace expansion or by listing items in a separate file referenced with --exclude-from. Glob patterns allow exclusions based on file type or naming structure, and combining --include and --exclude rules enables transfers limited to only matching files.
Before committing to a full transfer, running rsync with the --dry-run and --verbose flags allows users to preview what will and will not be copied without making any actual changes. The --cvs-exclude option provides a convenient shorthand for automatically skipping common temporary and build-related files during project directory syncs, though explicit rules are still recommended for modern version control directories such as those used by Git or Subversion. For more advanced scenarios, the --filter option extends beyond simple exclusion rules by supporting additional modifiers and merge-file functionality, following the same first-match logic as the standard include and exclude options.
10:26, 21st July 2021
Time Travel with py datatable 1.0
Version 1.0 of datatable, the Python counterpart to the widely used R package data.table, introduced support for temporal data types through two new formats and an accompanying family of functions. The date32 type represents a calendar date without a time component, storing values internally as a 32-bit signed integer counting days from the epoch date of 1 January 1970, with a range spanning approximately 5.8 million years in either direction. The time64 type captures a specific moment, stored as a 64-bit integer measuring nanoseconds from the same epoch in UTC.
Both types support initialisation in several ways, including from integer values, ISO 8601 formatted strings and individual date or time components via the constructor functions ymd() and ymdt(). When working with non-standard date strings, a combination of casting and string slicing functions within the datatable API can be used to parse and convert values correctly. The datatable.time family also includes a range of part functions such as year(), month(), day() and hour(), which allow users to extract individual components from date or time columns and apply them in operations such as filtering.
14:57, 15th July 2021
Get image size (width, height) with Python, OpenCV, Pillow (PIL)
In Python, image dimensions can be retrieved using either OpenCV or Pillow (PIL), with a key difference in how each library orders width and height. OpenCV treats images as NumPy arrays, where the shape attribute returns dimensions in the order of height, width and channel for colour images, or height and width for greyscale images, meaning width and height must be accessed by their respective index positions or via tuple unpacking.
Pillow, by contrast, offers a more straightforward approach through its size attribute, which returns a (width, height) tuple directly, as well as dedicated width and height attributes that can be accessed individually, and this behaviour is consistent across both colour and greyscale images.
14:56, 15th July 2021
Python Pandas ā Stop TruncatingĀ Strings
In the Python Pandas library, long strings are truncated by default when displayed, but this behaviour can be overridden by using the set_option function with the display.max_colwidth parameter set to -1, which prevents any truncation from occurring. This approach is considered cleaner than the common workaround of entering a large, arbitrary integer value to achieve the same result.
09:32, 15th July 2021
Reading and Writing XML Files in Python
Python offers two primary modules for handling XML files: the older minidom module and the more modern ElementTree module. Whilst minidom treats XML as a tree structure of objects based on the Document Object Model, ElementTree provides a more straightforward interface that represents XML data as simple lists and dictionaries, making it the more accessible option for those unfamiliar with DOM.
Using ElementTree, developers can parse existing XML files by creating a tree structure and accessing its root element, count child nodes, write new XML files by constructing elements and sub-elements, search for specific elements using functions such as find() and findall(), modify node content and attributes, add new sub-elements and remove individual attributes, specific sub-elements or entire groups of child nodes. Minidom is capable of parsing and counting XML elements too, but ElementTree is generally the recommended choice due to its simpler, more Pythonic approach and broader functionality.
09:31, 15th July 2021
How to Use sorted() and sort() in Python
Python offers two primary methods for sorting data: the built-in sorted() function and the .sort() list method. The sorted() function accepts any iterable as an argument and returns a new sorted list, leaving the original data unchanged, whilst .sort() operates directly on a list, modifying it in place and returning nothing.
Both methods support two optional keyword arguments, namely reverse, which accepts a Boolean value to switch between ascending and descending order, and key, which accepts a single-argument function to customise how elements are compared during sorting. The key argument can be used with built-in functions such as len() or str.lower(), or with lambda functions for more flexible sorting logic.
There are notable limitations to be aware of, including the inability to sort lists containing incompatible data types and the potential for errors when the function passed to key cannot handle all values in the iterable. Choosing between the two approaches depends largely on whether preserving the original data matters, as sorted() is the safer choice when the original order may still be needed, whilst .sort() is appropriate for lists where in-place modification is acceptable.
09:30, 15th July 2021
GNU sed is a stream editor, first released under the GNU Free Documentation Licence, that performs basic input transformations on files or pipeline input by making a single pass over the data, making it more efficient than interactive editors. It is invoked from the command line using a script and one or more input files, and supports a wide range of options including in-place file editing, extended regular expressions, sandbox mode and unbuffered input and output. Scripts consist of one or more commands, each optionally preceded by an address or address range that determines which lines the command acts upon, and commands can be separated by semicolons or newlines or grouped using curly braces.
The most commonly used command is the substitute command, which matches a regular expression against the pattern space and replaces matched content with a specified replacement string, supporting flags for global replacement, case-insensitive matching and output to a file. sed maintains two internal buffers, the pattern space and the hold space, and advanced scripting techniques make use of multi-line commands such as N, P and D to process multiple lines simultaneously, alongside branching commands such as b, t and T for flow control. Both basic and extended regular expression syntaxes are supported, with the latter enabled via the -E or -r option, and GNU sed additionally provides extensions including special character classes, back-references, escape sequences and multibyte character handling for use in localised environments.
09:15, 15th July 2021
How to conditionally stop SAS code execution and gracefully terminate SAS session
When developing SAS programmes that handle large datasets, there is often a need to stop code execution conditionally and terminate the SAS session without generating errors or warnings in the log. The ABORT statement, while useful for genuine failures, produces error messages that make it unsuitable for scenarios where stopping is a logical and expected outcome. The ENDSAS statement is a more appropriate tool, though it carries its own limitations, as it is a global statement that cannot be placed directly within conditional executable blocks such as IF-THEN logic without causing syntax errors.
Two reliable workarounds exist for achieving truly graceful termination. The first uses a data step with CALL EXECUTE to push the ENDSAS statement outside the step boundaries so that it executes conditionally after the step completes. The second uses SAS Macro Language to conditionally generate the ENDSAS statement alongside an informative note in the log.
For interactive development environments, capturing the SAS log to a file using PROC PRINTTO before any termination logic runs is strongly advisable, as closing the session will otherwise destroy the log output. Developers working in SAS Studio should be aware that ENDSAS behaves differently there, stopping further processing without terminating the session itself.