<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mansour Moufid</title>
    <description>The latest articles on DEV Community by Mansour Moufid (@eliteraspberries).</description>
    <link>https://dev.to/eliteraspberries</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F593925%2F7a9d6389-5b08-4107-a613-43e43b8f5d91.png</url>
      <title>DEV Community: Mansour Moufid</title>
      <link>https://dev.to/eliteraspberries</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eliteraspberries"/>
    <language>en</language>
    <item>
      <title>Unicode bug on macOS</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Sun, 30 Apr 2023 18:13:37 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/unicode-bug-on-macos-2558</link>
      <guid>https://dev.to/eliteraspberries/unicode-bug-on-macos-2558</guid>
      <description>&lt;p&gt;A diacritical mark is a mark on letter that conveys meaning like a change in pronunciation, like an accent. Diacritical marks in Unicode can have one or more encodings.&lt;/p&gt;

&lt;p&gt;For example the letter c with a cedilla (ç) can be the single character:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Character&lt;/th&gt;
&lt;th&gt;UTF-8 encoding&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ç&lt;/td&gt;
&lt;td&gt;0xc3 0xa7&lt;/td&gt;
&lt;td&gt;LATIN SMALL LETTER C WITH CEDILLA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;or as two:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Character&lt;/th&gt;
&lt;th&gt;UTF-8 encoding&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;c&lt;/td&gt;
&lt;td&gt;0x63&lt;/td&gt;
&lt;td&gt;LATIN SMALL LETTER C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0xcc 0xa7&lt;/td&gt;
&lt;td&gt;COMBINING CEDILLA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first is known as "normal form C" (NFC). The second is called "normal form D" (NFD) -- it is the &lt;em&gt;canonical decomposition&lt;/em&gt; of normal form C.&lt;/p&gt;

&lt;p&gt;Something interesting happens when you save a file with a name in NFC: macOS will not open it.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filename = 'français.txt'
with open(filename, 'wt') as f:
    print('Bonjour!', file=f)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Try to double-click this file in Finder. TextEdit will show in the Dock, but nothing else happens.&lt;/p&gt;

&lt;p&gt;Now close TextEdit, delete the file, and change the file name to its canonical decomposition (NFD):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import unicodedata

filename = 'français.txt'
filename = unicodedata.normalize('NFD', filename)

try:
    os.remove(filename)
except FileNotFoundError:
    pass
with open(filename, 'wt') as f:
    print('Bonjour!', file=f)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;TextEdit will open this file just fine.&lt;/p&gt;

&lt;p&gt;If you're wondering why a bunch of your files stopped opening after the latest upgrade to macOS (13.3.1), this is why...&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Make a macOS app icon from an image</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Wed, 15 Mar 2023 13:06:31 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/make-a-macos-icon-from-an-image-jod</link>
      <guid>https://dev.to/eliteraspberries/make-a-macos-icon-from-an-image-jod</guid>
      <description>&lt;p&gt;macOS has everything you need to make an icon file for your application from the terminal.&lt;/p&gt;

&lt;p&gt;macOS application icons are really a bundle ("iconset") of images at various sizes (16×16, 32×32, 64×64, ..., 1024×1024).&lt;/p&gt;

&lt;p&gt;First create a &lt;code&gt;myicon.iconset&lt;/code&gt; directory containing your icon image at all the sizes needed using the &lt;code&gt;sips&lt;/code&gt; command line tool:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sips -Z 16 myicon.png --out myicon/icon_16x16.png
sips -Z 32 myicon.png --out myicon/icon_16x16@2x.png
sips -Z 32 myicon.png --out myicon/icon_32x32.png
sips -Z 64 myicon.png --out myicon/icon_32x32@2x.png
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then bundle them together with the &lt;code&gt;iconutil&lt;/code&gt; command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iconutil -c icns myicon.iconset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here is a Makefile to do this automatically. Just save the Makefile to a directory containing your icon image file; change the variables &lt;code&gt;NAME&lt;/code&gt; and &lt;code&gt;ICON&lt;/code&gt; at the top; type &lt;code&gt;make&lt;/code&gt; in the Terminal, and you're done: &lt;a href="https://gist.github.com/eliteraspberries/a536f28b0498ec844fb210bb597738e2"&gt;https://gist.github.com/eliteraspberries/a536f28b0498ec844fb210bb597738e2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>macos</category>
      <category>design</category>
    </item>
    <item>
      <title>Quick bytes to integer (or other type) conversion in Python</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Tue, 20 Dec 2022 00:33:35 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/quick-bytes-to-integer-or-other-type-conversion-in-python-27lg</link>
      <guid>https://dev.to/eliteraspberries/quick-bytes-to-integer-or-other-type-conversion-in-python-27lg</guid>
      <description>&lt;p&gt;Sometimes you have a &lt;a href="https://docs.python.org/3/library/stdtypes.html#bytes"&gt;bytes&lt;/a&gt; object but what you really want is an integer.&lt;/p&gt;

&lt;p&gt;For example, I want the unsigned 32-bit integer represented by &lt;code&gt;b'1234'&lt;/code&gt;. I can do the conversion with &lt;a href="https://docs.python.org/3/library/struct.html#struct.unpack"&gt;&lt;code&gt;struct.unpack&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; struct.unpack('I', b'1234')
(875770417,)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or using the &lt;a href="https://docs.python.org/3/library/ctypes.html#ctypes._CData.from_buffer_copy"&gt;&lt;code&gt;from_buffer&lt;/code&gt;/&lt;code&gt;from_buffer_copy&lt;/code&gt;&lt;/a&gt; functions of the &lt;code&gt;ctypes&lt;/code&gt; module:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; ctypes.c_uint32.from_buffer_copy(b'1234')
c_uint(875770417)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is usually used for compound types (also called composite data types, i.e. types that are defined in terms of primitive data types, like a C structure), but still useful for simple things like integers.&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Programmatically create submodules in Python</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Tue, 11 Oct 2022 21:49:14 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/programmatically-create-submodules-in-python-4l86</link>
      <guid>https://dev.to/eliteraspberries/programmatically-create-submodules-in-python-4l86</guid>
      <description>&lt;p&gt;If you've ever wanted to distribute a Python &lt;a href="https://docs.python.org/3/tutorial/modules.html#packages"&gt;package&lt;/a&gt; as a single file, but keep the benefits of &lt;a href="https://en.wikipedia.org/wiki/Namespace#In_programming_languages"&gt;dotted namespaces&lt;/a&gt;, here is a tip.&lt;/p&gt;

&lt;p&gt;We can generate submodules programmatically and add them to the current module's namespace using &lt;a href="https://docs.python.org/3/library/importlib.html"&gt;importlib&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For example, here is a single file module (named &lt;code&gt;x&lt;/code&gt; if you save it to a file named &lt;code&gt;x.py&lt;/code&gt;) with a submodule named &lt;code&gt;y&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import importlib.machinery
import importlib.util
import sys

spec = importlib.machinery.ModuleSpec('y', None)
y = importlib.util.module_from_spec(spec)
sys.modules[__name__].y = y

y.abc = 123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In order to do &lt;code&gt;import x.y&lt;/code&gt; or &lt;code&gt;from x import y&lt;/code&gt; as if &lt;code&gt;x&lt;/code&gt; were a package and not simply a module, just add:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sys.modules[__name__ + '.y'] = y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Encode and decode FourCC codes, in Python</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Mon, 28 Mar 2022 23:37:33 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/encode-and-decode-fourcc-codes-in-python-3k6b</link>
      <guid>https://dev.to/eliteraspberries/encode-and-decode-fourcc-codes-in-python-3k6b</guid>
      <description>&lt;p&gt;Convert the string "H264" to an integer code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum([ord(c) &amp;lt;&amp;lt; (i * 8) for i, c in enumerate("H264")])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Convert the integer code (0x34363248) back to a string:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;''.join([chr((0x34363248 &amp;gt;&amp;gt; (i * 8)) &amp;amp; 0xff) for i in range(4)])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;See this gist: &lt;a href="https://gist.github.com/eliteraspberries/b017f998c97c395881f0d2f05a08ec60"&gt;https://gist.github.com/eliteraspberries/b017f998c97c395881f0d2f05a08ec60&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>video</category>
      <category>opencv</category>
    </item>
    <item>
      <title>Python unique list, in order</title>
      <dc:creator>Mansour Moufid</dc:creator>
      <pubDate>Tue, 07 Dec 2021 02:54:55 +0000</pubDate>
      <link>https://dev.to/eliteraspberries/python-unique-list-in-order-eg7</link>
      <guid>https://dev.to/eliteraspberries/python-unique-list-in-order-eg7</guid>
      <description>&lt;p&gt;Do you need to remove duplicates from a list, and keep it in order?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xs = [1, 3, 2, 2, 5, 4, 1, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset"&gt;set type&lt;/a&gt; will return one of each element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; set(xs)
{1, 2, 3, 4, 5}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But not in order, because a set is an &lt;em&gt;unordered collection&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; for x in set(xs):
...     print(x)
... 
1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, first construct a set from the list, then a list from this set, in the right order with the &lt;a href="https://docs.python.org/3/howto/sorting.html"&gt;sorted&lt;/a&gt; function, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; xs = [1, 3, 2, 2, 5, 4, 1, 5]
&amp;gt;&amp;gt;&amp;gt; list(sorted(set(xs), key=lambda x: xs.index(x)))
[1, 3, 2, 5, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quiz: When does the above construct &lt;em&gt;not&lt;/em&gt; work?&lt;/p&gt;

</description>
      <category>python</category>
    </item>
  </channel>
</rss>
