convert this python script from python 2 to 3
from os import path
class SvgImporter:
def __init__(self):
self.remove_overlap = True
self.import_outlines_flags = 'correctdir'
pass
def get_glyph_for_svg(self, font, filename):
char_code = self._get_char_code_from_svg(filename)
return font.create_char(char_code)
@staticmethod
def _get_char_code_from_svg(filename):
char = path.splitext(path.basename(filename))[0]
return ord(char)
def import_outlines_from_svg(self, glyph, filename):
glyph.importOutlines(filename, self.import_outlines_flags)
if self.remove_overlap:
glyph.removeOverlap()
We need to convert this script from Python 2 to 3. The script uses only standard library and no Python 2 specific constructs. But maybe some compatibility issues: ‘print’ not used, no division, no unicode vs str. The class methods use ‘pass’ at end of init, not needed. In Python 3, ‘path’ module is fine. ‘ord’ returns int. ‘filename’ string. So the code is already Python 3 compatible. But maybe they want to use relative imports or type hints. But the ask: convert this python script from python 2 to 3. So maybe we can add shebang, use from future import annotations? Not necessary. But perhaps they want to convert to a module with proper imports. The script uses ‘importOutlines’ method on glyph; not relevant.
In Python 2, ‘print’ is a statement, but no prints. The code may use old-style classes? It uses new style: class SvgImporter: inherits from object implicitly. So it’s fine.
Maybe they want to convert ‘path’ to ‘os.path’? It’s same.
So I think the answer: the script is already Python 3 compatible. But we can improve by adding type hints, using pathlib, and removing ‘pass’.
Let’s propose updated code:
#!/usr/bin/env python3 from pathlib import Path class SvgImporter: def __init__(self) -> None: self.remove_overlap: bool = True self.import_outlines_flags: str = 'correctdir' def get_glyph_for_svg(self, font, filename: str): char_code = self._get_char_code_from_svg(filename) return font.create_char(char_code) @staticmethod def _get_char_code_from_svg(filename: str) -> int: return ord(Path(filename).stem) def import_outlines_from_svg(self, glyph, filename: str): glyph.importOutlines(filename, self.import_outlines_flags) if self.remove_overlap: glyph.removeOverlap()
This uses pathlib for better path handling. Remove ‘pass’. Add type hints. Add shebang.
Also mention that code is already Python 3 ready.
Let’s answer accordingly.
The original snippet is already syntactically valid Python 3
– it only uses the standard library and old‑style print
statements, which you don’t have in the code.
Below is a small “Python 3‑friendly” rewrite that
pathlib.Path
over os.path
,pass
.#!/usr/bin/env python3
"""
A tiny helper to import SVG outlines into a font library.
The class itself is a thin wrapper around the glyph API that you
already have in your project. No changes are needed for the
glyph side‑of‑the‑story – this file is now fully Python 3‑ready.
"""
from pathlib import Path
from typing import Any
class SvgImporter:
"""Import SVG glyphs into a font."""
def __init__(self) -> None:
# Whether to call glyph.removeOverlap() after importing.
self.remove_overlap: bool = True
# Flag string that is passed to the glyph's importOutlines method.
self.import_outlines_flags: str = "correctdir"
# ------------------------------------------------------------------ #
# Public helpers
# ------------------------------------------------------------------ #
def get_glyph_for_svg(self, font: Any, filename: str):
"""
Ask *font* to create a new character whose code point is taken from
the SVG file name.
Parameters
----------
font : Any
The font object that provides ``create_char``.
filename : str
Full path to the SVG file. The file name (without extension)
must be a single character – e.g. ``'A.svg'``.
"""
= self._get_char_code_from_svg(filename)
char_code return font.create_char(char_code)
def import_outlines_from_svg(self, glyph: Any, filename: str):
"""
Import the outlines from *filename* into *glyph*.
Parameters
----------
glyph : Any
Glyph object that provides ``importOutlines`` and ``removeOverlap``.
filename : str
Path to the SVG file.
"""
self.import_outlines_flags)
glyph.importOutlines(filename,
if self.remove_overlap:
glyph.removeOverlap()
# ------------------------------------------------------------------ #
# Static helpers
# ------------------------------------------------------------------ #
@staticmethod
def _get_char_code_from_svg(filename: str) -> int:
"""
Extract the code point from the SVG file name.
The file name (without extension) must be a single character; e.g.
``'A.svg'`` → ord('A') == 65.
"""
return ord(Path(filename).stem)
# ---------------------------------------------------------------------- #
# If you run this file directly, demonstrate a very small example
# (this will fail unless you have a compatible font/glyph implementation).
# ---------------------------------------------------------------------- #
if __name__ == "__main__":
# Dummy stubs – replace with your actual font/glyph objects.
class DummyFont:
def create_char(self, code):
print(f"Created character 0x{code:04x}")
class DummyGlyph:
def importOutlines(self, path, flags):
print(f"Imported outlines from {path!r} with flags {flags!r}")
def removeOverlap(self):
print("Removed overlaps")
= SvgImporter()
importer = DummyFont()
dummy_font = DummyGlyph()
dummy_glyph
# Example usage
"A.svg")
importer.get_glyph_for_svg(dummy_font, "B.svg") importer.import_outlines_from_svg(dummy_glyph,
Old | New | Why |
---|---|---|
from os import path |
from pathlib import Path |
pathlib gives a cleaner, object‑oriented API and is the
modern way to handle paths. |
pass in __init__ |
removed | It was unnecessary. |
No type hints | Added -> None , -> int ,
Any |
Helpful for static type checkers and IDEs. |
Added a shebang (#!/usr/bin/env python3 ) |
– | Allows the file to be executed directly on Unix‑like systems. |
Minor docstring & comment improvements | – | Makes the code self‑documenting. |
The logic and behaviour are unchanged – the class will still work with your existing font/glyph infrastructure. If you encounter any runtime issues, they are almost certainly due to the external library rather than the conversion itself.